Index: lib/fuzzer/afl/afl_driver.cpp =================================================================== --- lib/fuzzer/afl/afl_driver.cpp +++ lib/fuzzer/afl/afl_driver.cpp @@ -59,6 +59,7 @@ #include #include #include +#include #include #include @@ -305,6 +306,19 @@ return 0; } +static int set_iterations(int *N, const char *arg) { + char *next_char; + long NL = strtol(arg, &next_char, 10); + if (NL < 1 || NL > INT_MAX || *next_char != '\0') { + fprintf(stderr, "WARNING: iterations invalid `%s`, default to %d\n", + arg, *N); + return -1; + } + + *N = static_cast(NL); + return 0; +} + int main(int argc, char **argv) { fprintf(stderr, "======================= INFO =========================\n" @@ -331,11 +345,11 @@ int N = 1000; if (argc == 2 && argv[1][0] == '-') - N = atoi(argv[1] + 1); - else if(argc == 2 && (N = atoi(argv[1])) > 0) - fprintf(stderr, "WARNING: using the deprecated call style `%s %d`\n", - argv[0], N); - else if (argc > 1) + set_iterations(&N, argv[1] + 1); + else if(argc == 2 && set_iterations(&N, argv[1]) == 0) { + fprintf(stderr, "WARNING: using the deprecated call style `%s %d`\n", + argv[0], N); + } else if (argc > 1) return ExecuteFilesOnyByOne(argc, argv); assert(N > 0); Index: test/fuzzer/afl-driver.test =================================================================== --- test/fuzzer/afl-driver.test +++ test/fuzzer/afl-driver.test @@ -27,3 +27,9 @@ RUN: %run %t-AFLDriverTest %t.file3 %t.file4 2>&1 | FileCheck %s --check-prefix=CHECK5 CHECK5: LLVMFuzzerTestOneInput called; Size = 3 CHECK5: LLVMFuzzerTestOneInput called; Size = 4 + +RUN: %run %t-AFLDriverTest < %t.file3 --1 2>&1 | FileCheck %s --check-prefix=CHECK6 +CHECK6: WARNING: iterations invalid `-1`, default to 1000 + +RUN: %run %t-AFLDriverTest < %t.file3 -Invalid 2>&1 | FileCheck %s --check-prefix=CHECK7 +CHECK7: WARNING: iterations invalid `Invalid`, default to 1000