discontinue use of alloca

NetBSD emits:
warning: Warning: reference to the libc supplied alloca(3); this most likely will not work. Please use the compiler provided version of alloca(3), by supplying the appropriate compiler flags (e.g. not -std=c89).

and man 3 alloca says:

       Normally,  gcc(1)  translates  calls  to  alloca()  with  inlined code.  This is not done when either the -ansi, -std=c89, -std=c99, or the
       -std=c11 option is given and the header <alloca.h> is not included.  Otherwise, (without an -ansi or -std=c* option) the glibc  version  of
       <stdlib.h> includes <alloca.h> and that contains the lines:

           #ifdef  __GNUC__
           #define alloca(size)   __builtin_alloca (size)
           #endif

It looks like alloca is a bad idea in modern C/C++, so we use
VLAs for C and std::vector for C++.
This commit is contained in:
moneromooo-monero 2019-02-23 12:00:41 +00:00
parent 31bdf7bd11
commit 7d88d8f27c
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 4 additions and 14 deletions

View file

@ -91,7 +91,7 @@ int spawn(const char *filename, const std::vector<std::string>& args, bool wait)
MINFO("Child exited with " << exitCode); MINFO("Child exited with " << exitCode);
return static_cast<int>(exitCode); return static_cast<int>(exitCode);
#else #else
char **argv = (char**)alloca(sizeof(char*) * (args.size() + 1)); std::vector<char*> argv(args.size() + 1);
for (size_t n = 0; n < args.size(); ++n) for (size_t n = 0; n < args.size(); ++n)
argv[n] = (char*)args[n].c_str(); argv[n] = (char*)args[n].c_str();
argv[args.size()] = NULL; argv[args.size()] = NULL;
@ -109,7 +109,7 @@ int spawn(const char *filename, const std::vector<std::string>& args, bool wait)
tools::closefrom(3); tools::closefrom(3);
close(0); close(0);
char *envp[] = {NULL}; char *envp[] = {NULL};
execve(filename, argv, envp); execve(filename, argv.data(), envp);
MERROR("Failed to execve: " << strerror(errno)); MERROR("Failed to execve: " << strerror(errno));
return -1; return -1;
} }

View file

@ -34,15 +34,6 @@
#include "hash-ops.h" #include "hash-ops.h"
#ifdef _MSC_VER
#include <malloc.h>
#elif !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__) \
&& !defined(__NetBSD__)
#include <alloca.h>
#else
#include <stdlib.h>
#endif
/*** /***
* Round to power of two, for count>=3 and for count being not too large (as reasonable for tree hash calculations) * Round to power of two, for count>=3 and for count being not too large (as reasonable for tree hash calculations)
*/ */
@ -91,9 +82,8 @@ void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash) {
size_t cnt = tree_hash_cnt( count ); size_t cnt = tree_hash_cnt( count );
char (*ints)[HASH_SIZE]; char ints[cnt][HASH_SIZE];
size_t ints_size = cnt * HASH_SIZE; memset(ints, 0 , sizeof(ints)); // zero out as extra protection for using uninitialized mem
ints = alloca(ints_size); memset( ints , 0 , ints_size); // allocate, and zero out as extra protection for using uninitialized mem
memcpy(ints, hashes, (2 * cnt - count) * HASH_SIZE); memcpy(ints, hashes, (2 * cnt - count) * HASH_SIZE);