From c3e22b7581f49e3834df5b87bc81bc80e6c0a5e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 12 Jan 2015 10:15:32 +0100 Subject: [PATCH] freebsd: make uv_exepath more resilient PR-URL: https://github.com/libuv/libuv/pull/129 Reviewed-By: Ben Noordhuis --- src/unix/freebsd.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/unix/freebsd.c b/src/unix/freebsd.c index 55492adc..d87b74b9 100644 --- a/src/unix/freebsd.c +++ b/src/unix/freebsd.c @@ -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; }