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