1 // Copyright 2007-2019 The JsonCpp Authors 2 // Distributed under MIT license, or public domain if desired and 3 // recognized in your jurisdiction. 4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE 5 6 #include "fuzz.h" 7 8 #include <json/config.h> 9 #include <json/json.h> 10 #include <memory> 11 #include <string> 12 13 namespace Json { 14 class Exception; 15 } 16 LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)17extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 18 Json::CharReaderBuilder builder; 19 20 if (size < sizeof(uint32_t)) { 21 return 0; 22 } 23 24 const uint32_t hash_settings = static_cast<uint32_t>(data[0]) | 25 (static_cast<uint32_t>(data[1]) << 8) | 26 (static_cast<uint32_t>(data[2]) << 16) | 27 (static_cast<uint32_t>(data[3]) << 24); 28 data += sizeof(uint32_t); 29 size -= sizeof(uint32_t); 30 31 builder.settings_["failIfExtra"] = hash_settings & (1 << 0); 32 builder.settings_["allowComments_"] = hash_settings & (1 << 1); 33 builder.settings_["strictRoot_"] = hash_settings & (1 << 2); 34 builder.settings_["allowDroppedNullPlaceholders_"] = hash_settings & (1 << 3); 35 builder.settings_["allowNumericKeys_"] = hash_settings & (1 << 4); 36 builder.settings_["allowSingleQuotes_"] = hash_settings & (1 << 5); 37 builder.settings_["failIfExtra_"] = hash_settings & (1 << 6); 38 builder.settings_["rejectDupKeys_"] = hash_settings & (1 << 7); 39 builder.settings_["allowSpecialFloats_"] = hash_settings & (1 << 8); 40 builder.settings_["collectComments"] = hash_settings & (1 << 9); 41 builder.settings_["allowTrailingCommas_"] = hash_settings & (1 << 10); 42 43 Json::CharReader* reader(builder.newCharReader()); 44 Json::Value root; 45 const char* data_str = reinterpret_cast<const char*>(data); 46 try { 47 reader->parse(data_str, data_str + size, &root, JSONCPP_NULL); 48 } catch (Json::Exception const&) { 49 } 50 delete reader; 51 // Whether it succeeded or not doesn't matter. 52 return 0; 53 } 54