unix: handle invalid _SC_GETPW_R_SIZE_MAX values

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 <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
cjihrig 2015-06-04 18:02:42 -04:00 committed by Saúl Ibarra Corretgé
parent f198b82f59
commit d82e47a60f

View File

@ -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;