diff --git a/fuzzing/CMakeLists.txt b/fuzzing/CMakeLists.txt index d95be8548ef4b695228467820aa5f936e1941634..d39a6521df8ed28102cbe897d9556c42ee2ff115 100644 --- a/fuzzing/CMakeLists.txt +++ b/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) diff --git a/fuzzing/afl.c b/fuzzing/afl.c index 28c7e40c8f7c5dc09ed71e579e2922477b4256f7..f2452def260ae85723172126ab8affde01e46d61 100644 --- a/fuzzing/afl.c +++ b/fuzzing/afl.c @@ -22,6 +22,7 @@ #include #include +#include #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; }