From d82e47a60fd42ba30c1299b98033443ae7fc1f78 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 4 Jun 2015 18:02:42 -0400 Subject: [PATCH] unix: handle invalid _SC_GETPW_R_SIZE_MAX values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some systems, FreeBSD for example, may return negative values. sysconf() returns a long, which was being converted to a size_t. This conversion lead to large allocations, and subsequent out of memory failures. This commit checks the long value returned by sysconf() properly, and uses a default value of 4096 if a negative number is returned. PR-URL: https://github.com/libuv/libuv/pull/389 Reviewed-By: Ben Noordhuis Reviewed-By: Saúl Ibarra Corretgé --- src/unix/core.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/unix/core.c b/src/unix/core.c index cfb76304..826b4113 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -1000,6 +1000,7 @@ int uv_os_homedir(char* buffer, size_t* size) { uid_t uid; size_t bufsize; size_t len; + long initsize; int r; if (buffer == NULL || size == NULL || *size == 0) @@ -1023,10 +1024,12 @@ int uv_os_homedir(char* buffer, size_t* size) { } /* HOME is not set, so call getpwuid() */ - bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); + initsize = sysconf(_SC_GETPW_R_SIZE_MAX); - if (bufsize <= 0) - return -EIO; + if (initsize <= 0) + bufsize = 4096; + else + bufsize = (size_t) initsize; uid = getuid(); buf = NULL;