ext/libuv/src/unix/freebsd.c in libuv-1.1.3 vs ext/libuv/src/unix/freebsd.c in libuv-1.2.0
- old
+ new
@@ -73,14 +73,15 @@
return (((uint64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec);
}
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)
+ if (buffer == NULL || size == NULL || *size == 0)
return -EINVAL;
#ifdef __DragonFly__
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
@@ -91,13 +92,22 @@
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
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;
}