freebsd: make uv_exepath more resilient

PR-URL: https://github.com/libuv/libuv/pull/129
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Saúl Ibarra Corretgé 2015-01-12 10:15:32 +01:00
parent 434ce034e0
commit c3e22b7581

View File

@ -75,8 +75,9 @@ uint64_t uv__hrtime(uv_clocktype_t type) {
int uv_exepath(char* buffer, size_t* size) {
char abspath[PATH_MAX * 2 + 1];
int mib[4];
size_t cb;
size_t abspath_size;
if (buffer == NULL || size == NULL || *size == 0)
return -EINVAL;
@ -93,10 +94,19 @@ int uv_exepath(char* buffer, size_t* size) {
mib[3] = -1;
#endif
cb = *size;
if (sysctl(mib, 4, buffer, &cb, NULL, 0))
abspath_size = sizeof abspath;;
if (sysctl(mib, 4, abspath, &abspath_size, NULL, 0))
return -errno;
*size = strlen(buffer);
assert(abspath_size > 0);
abspath_size -= 1;
*size -= 1;
if (*size > abspath_size)
*size = abspath_size;
memcpy(buffer, abspath, *size);
buffer[*size] = '\0';
return 0;
}