Debian Codegraph @

src/test_lib_json/fuzz.cpp

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