dtoolutil: fix buffer overrun on FreeBSD extracting long cmdline args

This happens when compiling with CMake, which passes very long command-lines.
This commit is contained in:
rdb 2020-05-05 13:20:20 +02:00
parent b5d0e9eafc
commit 68d094dba4

View File

@ -748,11 +748,14 @@ read_args() {
#elif defined(IS_FREEBSD) #elif defined(IS_FREEBSD)
// In FreeBSD, we can use sysctl to determine the command-line arguments. // In FreeBSD, we can use sysctl to determine the command-line arguments.
size_t bufsize = 4096; size_t bufsize = 0;
char buffer[4096];
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0}; int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0};
mib[3] = getpid(); mib[3] = getpid();
if (sysctl(mib, 4, (void*) buffer, &bufsize, nullptr, 0) == -1) { if (sysctl(mib, 4, nullptr, &bufsize, nullptr, 0) == -1) {
perror("sysctl");
} else {
char *buffer = (char *)alloca(bufsize);
if (sysctl(mib, 4, buffer, &bufsize, nullptr, 0) == -1) {
perror("sysctl"); perror("sysctl");
} else { } else {
if (_binary_name.empty()) { if (_binary_name.empty()) {
@ -765,6 +768,7 @@ read_args() {
idx += newidx + 1; idx += newidx + 1;
} }
} }
}
#elif defined(HAVE_GLOBAL_ARGV) #elif defined(HAVE_GLOBAL_ARGV)
int argc = GLOBAL_ARGC; int argc = GLOBAL_ARGC;