|
| 1 | +// Copyright 2007-2010 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 <bits/stdint-uintn.h> |
| 9 | +#include <json/config.h> |
| 10 | +#include <json/json.h> |
| 11 | +#include <memory> |
| 12 | +#include <stdint.h> |
| 13 | +#include <string> |
| 14 | + |
| 15 | +namespace Json { |
| 16 | +class Exception; |
| 17 | +} |
| 18 | + |
| 19 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 20 | + Json::CharReaderBuilder builder; |
| 21 | + |
| 22 | + if (size < sizeof(uint32_t)) { |
| 23 | + return 0; |
| 24 | + } |
| 25 | + |
| 26 | + uint32_t hash_settings = *(const uint32_t*)data; |
| 27 | + data += sizeof(uint32_t); |
| 28 | + |
| 29 | + builder.settings_["failIfExtra"] = hash_settings & (1 << 0); |
| 30 | + builder.settings_["allowComments_"] = hash_settings & (1 << 1); |
| 31 | + builder.settings_["strictRoot_"] = hash_settings & (1 << 2); |
| 32 | + builder.settings_["allowDroppedNullPlaceholders_"] = hash_settings & (1 << 3); |
| 33 | + builder.settings_["allowNumericKeys_"] = hash_settings & (1 << 4); |
| 34 | + builder.settings_["allowSingleQuotes_"] = hash_settings & (1 << 5); |
| 35 | + builder.settings_["failIfExtra_"] = hash_settings & (1 << 6); |
| 36 | + builder.settings_["rejectDupKeys_"] = hash_settings & (1 << 7); |
| 37 | + builder.settings_["allowSpecialFloats_"] = hash_settings & (1 << 8); |
| 38 | + |
| 39 | + std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
| 40 | + |
| 41 | + Json::Value root; |
| 42 | + const char* data_str = reinterpret_cast<const char*>(data); |
| 43 | + try { |
| 44 | + reader->parse(data_str, data_str + size, &root, nullptr); |
| 45 | + } catch (Json::Exception const&) { |
| 46 | + } |
| 47 | + // Whether it succeeded or not doesn't matter. |
| 48 | + return 0; |
| 49 | +} |
0 commit comments