Browse Source

fuzzing: Fuzz printing as well.

With one big limitation: It can only be fuzzed with what has been
parsed by the library beforehand.
Max Bruckner 8 years ago
parent
commit
4785070ad3
2 changed files with 35 additions and 6 deletions
  1. 7 2
      fuzzing/CMakeLists.txt
  2. 28 4
      fuzzing/afl.c

+ 7 - 2
fuzzing/CMakeLists.txt

@@ -5,7 +5,6 @@ if (ENABLE_FUZZING)
         message(FATAL_ERROR "Couldn't find afl-fuzz.")
     endif()
 
-
     add_executable(afl-main afl.c)
     target_link_libraries(afl-main "${CJSON_LIB}")
 
@@ -13,8 +12,14 @@ if (ENABLE_FUZZING)
         message(FATAL_ERROR "Enable sanitizers with -DENABLE_SANITIZERS=On to do fuzzing.")
     endif()
 
+    option(ENABLE_FUZZING_PRINT "Fuzz printing functions together with parser." On)
+    set(fuzz_print_parameter "no")
+    if (ENABLE_FUZZING_PRINT)
+        set(fuzz_print_parameter "yes")
+    endif()
+
     add_custom_target(afl
-        COMMAND "${AFL_FUZZ}" -i "${CMAKE_CURRENT_SOURCE_DIR}/inputs" -o "${CMAKE_CURRENT_BINARY_DIR}/findings" -x "${CMAKE_CURRENT_SOURCE_DIR}/json.dict" -- "${CMAKE_CURRENT_BINARY_DIR}/afl-main" "@@"
+        COMMAND "${AFL_FUZZ}" -i "${CMAKE_CURRENT_SOURCE_DIR}/inputs" -o "${CMAKE_CURRENT_BINARY_DIR}/findings" -x "${CMAKE_CURRENT_SOURCE_DIR}/json.dict" -- "${CMAKE_CURRENT_BINARY_DIR}/afl-main" "@@" "${fuzz_print_parameter}"
         DEPENDS afl-main)
 
 

+ 28 - 4
fuzzing/afl.c

@@ -22,6 +22,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "../cJSON.h"
 
@@ -86,23 +87,42 @@ int main(int argc, char** argv)
     const char *filename = NULL;
     cJSON *item = NULL;
     char *json = NULL;
+    int status = EXIT_SUCCESS;
+    char *printed_json = NULL;
 
-    if (argc < 2)
+    if ((argc < 2) || (argc > 3))
     {
         printf("Usage:\n");
-        printf("%s input_file\n", argv[0]);
-        printf("\t input_file: file containing the test data");
+        printf("%s input_file [enable_printing]\n", argv[0]);
+        printf("\t input_file: file containing the test data\n");
+        printf("\t enable_printing: print after parsing, 'yes' or 'no', defaults to 'no'\n");
     }
 
     filename = argv[1];
 
     json = read_file(filename);
+    if (json == NULL)
+    {
+        status = EXIT_FAILURE;
+        goto cleanup;
+    }
     item = cJSON_Parse(json);
     if (item == NULL)
     {
         goto cleanup;
     }
 
+    if ((argc == 3) && (strncmp(argv[2], "yes", 3) == 0))
+    {
+        printed_json = cJSON_Print(item);
+        if (printed_json == NULL)
+        {
+            status = EXIT_FAILURE;
+            goto cleanup;
+        }
+        printf("%s\n", printed_json);
+    }
+
 cleanup:
     if (item != NULL)
     {
@@ -112,6 +132,10 @@ cleanup:
     {
         free(json);
     }
+    if (printed_json != NULL)
+    {
+        free(printed_json);
+    }
 
-    return EXIT_SUCCESS;
+    return status;
 }