diff options
author | Daniel Salzman <daniel.salzman@nic.cz> | 2017-10-09 09:56:21 +0200 |
---|---|---|
committer | Daniel Salzman <daniel.salzman@nic.cz> | 2017-10-09 09:56:21 +0200 |
commit | 5094d2382e938df00bff3d0b38fd45a494b12329 (patch) | |
tree | 98fad7b130882b41a80c01aa6a614bbfe02ea8f2 /tests-fuzz/main.c | |
parent | doc: remove obsolete warning for zonefile-sync (diff) | |
download | knot-5094d2382e938df00bff3d0b38fd45a494b12329.tar.xz knot-5094d2382e938df00bff3d0b38fd45a494b12329.zip |
tests-fuzz: fix unchecked return + refactoring
Diffstat (limited to 'tests-fuzz/main.c')
-rw-r--r-- | tests-fuzz/main.c | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/tests-fuzz/main.c b/tests-fuzz/main.c index 70d2b2446..a145306af 100644 --- a/tests-fuzz/main.c +++ b/tests-fuzz/main.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> * Copyright(c) 2017 Tim Ruehsen * * Permission is hereby granted, free of charge, to any person obtaining a @@ -20,6 +21,7 @@ * DEALINGS IN THE SOFTWARE. */ +#include <assert.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> @@ -37,46 +39,52 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); static void test_all_from(const char *dirname) { - DIR *dirp; + DIR *dirp = opendir(dirname); + if (dirp == NULL) { + return; + } + struct dirent *dp; + while ((dp = readdir(dirp))) { + if (*dp->d_name == '.') { + continue; + } - if ((dirp = opendir(dirname))) { - while ((dp = readdir(dirp))) { - if (*dp->d_name == '.') continue; - - char fname[strlen(dirname) + strlen(dp->d_name) + 2]; - snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name); - - int fd; - if ((fd = open(fname, O_RDONLY)) == -1) { - fprintf(stderr, "Failed to open %s (%d)\n", fname, errno); - continue; - } - - struct stat st; - if (fstat(fd, &st) != 0) { - fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno); - close(fd); - continue; - } - - uint8_t *data = malloc(st.st_size); - ssize_t n; - if ((n = read(fd, data, st.st_size)) == st.st_size) { - printf("testing %llu bytes from '%s'\n", (unsigned long long) st.st_size, fname); - fflush(stdout); - LLVMFuzzerTestOneInput(data, st.st_size); - fflush(stderr); - } else { - fprintf(stderr, "Failed to read %llu bytes from %s (%d), got %zd\n", - (unsigned long long) st.st_size, fname, errno, n); - } - - free(data); + char fname[strlen(dirname) + strlen(dp->d_name) + 2]; + int ret = snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name); + assert(ret > 0 && ret < sizeof(fname)); + + int fd; + if ((fd = open(fname, O_RDONLY)) == -1) { + fprintf(stderr, "Failed to open %s (%d)\n", fname, errno); + continue; + } + + struct stat st; + if (fstat(fd, &st) != 0) { + fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno); close(fd); + continue; + } + + uint8_t *data = malloc(st.st_size); + assert(data); + + ssize_t n; + if ((n = read(fd, data, st.st_size)) == st.st_size) { + printf("testing %llu bytes from '%s'\n", (unsigned long long) st.st_size, fname); + fflush(stdout); + LLVMFuzzerTestOneInput(data, st.st_size); + fflush(stderr); + } else { + fprintf(stderr, "Failed to read %llu bytes from %s (%d), got %zd\n", + (unsigned long long) st.st_size, fname, errno, n); } - closedir(dirp); + + free(data); + close(fd); } + closedir(dirp); } int main(int argc, char **argv) @@ -90,11 +98,13 @@ int main(int argc, char **argv) target += 3; } - snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.in", target); + int ret = snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.in", target); + assert(ret > 0 && ret < sizeof(corporadir)); test_all_from(corporadir); - snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.repro", target); + ret = snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.repro", target); + assert(ret > 0 && ret < sizeof(corporadir)); test_all_from(corporadir); |