Remove alloca from fuzz/main.c

This commit is contained in:
Tim Rühsen 2019-06-02 13:07:35 +02:00
parent 5131ef0d5d
commit f32c240cc0
1 changed files with 10 additions and 21 deletions

View File

@ -40,6 +40,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h>
#if defined (_MSC_VER) && ! defined (ssize_t) #if defined (_MSC_VER) && ! defined (ssize_t)
#include <basetsd.h> #include <basetsd.h>
@ -50,11 +51,6 @@ typedef SSIZE_T ssize_t;
#if defined (TEST_RUN) && defined (HAVE_FMEMOPEN) #if defined (TEST_RUN) && defined (HAVE_FMEMOPEN)
#include <dirent.h>
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#endif
static void test_all_from(const char *dirname) static void test_all_from(const char *dirname)
{ {
DIR *dirp; DIR *dirp;
@ -62,8 +58,7 @@ static void test_all_from(const char *dirname)
if ((dirp = opendir(dirname))) { if ((dirp = opendir(dirname))) {
while ((dp = readdir(dirp))) { while ((dp = readdir(dirp))) {
size_t fnamesize; char fname[1024];
char *fname;
int fd; int fd;
struct stat st; struct stat st;
uint8_t *data; uint8_t *data;
@ -71,9 +66,7 @@ static void test_all_from(const char *dirname)
if (*dp->d_name == '.') continue; if (*dp->d_name == '.') continue;
fnamesize = strlen(dirname) + strlen(dp->d_name) + 2; snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name);
fname = alloca(fnamesize);
snprintf(fname, fnamesize, "%s/%s", dirname, dp->d_name);
if ((fd = open(fname, O_RDONLY)) == -1) { if ((fd = open(fname, O_RDONLY)) == -1) {
fprintf(stderr, "Failed to open %s (%d)\n", fname, errno); fprintf(stderr, "Failed to open %s (%d)\n", fname, errno);
@ -88,7 +81,7 @@ static void test_all_from(const char *dirname)
data = malloc(st.st_size); data = malloc(st.st_size);
if ((n = read(fd, data, st.st_size)) == st.st_size) { if ((n = read(fd, data, st.st_size)) == st.st_size) {
printf("testing %u bytes from '%s'\n", (int) st.st_size, fname); printf("testing %d bytes from '%s'\n", (int) n, fname);
LLVMFuzzerTestOneInput(data, st.st_size); LLVMFuzzerTestOneInput(data, st.st_size);
} else } else
fprintf(stderr, "Failed to read %d bytes from %s (%d), got %d\n", (int) st.st_size, fname, errno, (int) n); fprintf(stderr, "Failed to read %d bytes from %s (%d), got %d\n", (int) st.st_size, fname, errno, (int) n);
@ -102,31 +95,27 @@ static void test_all_from(const char *dirname)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
const char *target;
size_t corporadirsize = sizeof(SRCDIR) + 1 + strlen(argv[0]) + 8;
char *corporadir = alloca(corporadirsize);
/* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */ /* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
if (argc == 1) { if (argc == 1) {
const char *valgrind = getenv("TESTS_VALGRIND"); const char *valgrind = getenv("TESTS_VALGRIND");
if (valgrind && *valgrind) { if (valgrind && *valgrind) {
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32; char cmd[1024];
char *cmd = alloca(cmdsize);
snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]); snprintf(cmd, sizeof(cmd), "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
return system(cmd) != 0; return system(cmd) != 0;
} }
} }
target = strrchr(argv[0], '/'); const char *target = strrchr(argv[0], '/');
target = target ? target + 1 : argv[0]; target = target ? target + 1 : argv[0];
snprintf(corporadir, corporadirsize, SRCDIR "/%s.in", target); char corporadir[1024];
snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.in", target);
test_all_from(corporadir); test_all_from(corporadir);
snprintf(corporadir, corporadirsize, SRCDIR "/%s.repro", target); snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.repro", target);
test_all_from(corporadir); test_all_from(corporadir);