diff --git a/src/test_lib_json/fuzz.cpp b/src/test_lib_json/fuzz.cpp index f79f19ffe..30dc6539e 100644 --- a/src/test_lib_json/fuzz.cpp +++ b/src/test_lib_json/fuzz.cpp @@ -3,47 +3,31 @@ // recognized in your jurisdiction. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE -#include "fuzz.h" - -#include -#include -#include -#include -#include +#include #include - -namespace Json { -class Exception; -} - -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - Json::CharReaderBuilder builder; - - if (size < sizeof(uint32_t)) { +#include "fuzz.h" +#include "json/config.h" +#include "json/features.h" +#include "json/reader.h" +#include "json/value.h" +#include "json/writer.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + const std::string json_string(reinterpret_cast(data), size); + Json::Reader reader(Json::Features::strictMode()); + Json::Value value; + const bool success = reader.parse(json_string, value, false); + if (!success) { return 0; } - uint32_t hash_settings = *(const uint32_t*)data; - data += sizeof(uint32_t); - - builder.settings_["failIfExtra"] = hash_settings & (1 << 0); - builder.settings_["allowComments_"] = hash_settings & (1 << 1); - builder.settings_["strictRoot_"] = hash_settings & (1 << 2); - builder.settings_["allowDroppedNullPlaceholders_"] = hash_settings & (1 << 3); - builder.settings_["allowNumericKeys_"] = hash_settings & (1 << 4); - builder.settings_["allowSingleQuotes_"] = hash_settings & (1 << 5); - builder.settings_["failIfExtra_"] = hash_settings & (1 << 6); - builder.settings_["rejectDupKeys_"] = hash_settings & (1 << 7); - builder.settings_["allowSpecialFloats_"] = hash_settings & (1 << 8); + // Write with StyledWriter + Json::StyledWriter styled_writer; + styled_writer.write(value); - std::unique_ptr reader(builder.newCharReader()); - - Json::Value root; - const char* data_str = reinterpret_cast(data); - try { - reader->parse(data_str, data_str + size, &root, nullptr); - } catch (Json::Exception const&) { - } - // Whether it succeeded or not doesn't matter. + // Write with StyledStreamWriter + Json::StyledStreamWriter styled_stream_writer; + JSONCPP_OSTRINGSTREAM sstream; + styled_stream_writer.write(sstream, value); return 0; } diff --git a/src/test_lib_json/fuzz.dict b/src/test_lib_json/fuzz.dict new file mode 100644 index 000000000..725423d2f --- /dev/null +++ b/src/test_lib_json/fuzz.dict @@ -0,0 +1,54 @@ +# +# AFL dictionary for JSON +# ----------------------- +# +# Just the very basics. +# +# Inspired by a dictionary by Jakub Wilk +# +# https://github.com/rc0r/afl-fuzz/blob/master/dictionaries/json.dict +# + +"0" +",0" +":0" +"0:" +"-1.2e+3" + +"true" +"false" +"null" + +"\"\"" +",\"\"" +":\"\"" +"\"\":" + +"{}" +",{}" +":{}" +"{\"\":0}" +"{{}}" + +"[]" +",[]" +":[]" +"[0]" +"[[]]" + +"''" +"\\" +"\\b" +"\\f" +"\\n" +"\\r" +"\\t" +"\\u0000" +"\\x00" +"\\0" +"\\uD800\\uDC00" +"\\uDBFF\\uDFFF" + +"\"\":0" +"//" +"/**/"