[Fuzzer] Afl driver changing iterations handling

Handling differently the iterations with the type limit and eventually an error message.

Reviewers: morehouse, kcc

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D47880

llvm-svn: 334510
GitOrigin-RevId: 301855fb0d9398b84718f5ca34c9a55ad11aa14e
diff --git a/afl/afl_driver.cpp b/afl/afl_driver.cpp
index fa494c0..f3435ff 100644
--- a/afl/afl_driver.cpp
+++ b/afl/afl_driver.cpp
@@ -59,6 +59,7 @@
 #include <sys/resource.h>
 #include <sys/time.h>
 #include <unistd.h>
+#include <limits.h>
 
 #include <fstream>
 #include <iostream>
@@ -305,6 +306,18 @@
   return 0;
 }
 
+static void 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`\n",
+        arg);
+    ::exit(-1);
+  }
+
+  *N = static_cast<int>(NL);
+}
+
 int main(int argc, char **argv) {
   fprintf(stderr,
       "======================= INFO =========================\n"
@@ -331,11 +344,12 @@
 
   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) {
+    fprintf(stderr, "WARNING: using the deprecated call style `%s %d`\n",
+        argv[0], N);
+    set_iterations(&N, argv[1]);
+  } else if (argc > 1)
     return ExecuteFilesOnyByOne(argc, argv);
 
   assert(N > 0);