/src/jsoncpp_fuzz_proto.cc
Line | Count | Source |
1 | | // Copyright 2020 Google Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include "json.pb.h" |
18 | | #include "json_proto_converter.h" |
19 | | #include "libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h" |
20 | | |
21 | | #include <cstdint> |
22 | | #include <json/config.h> |
23 | | #include <json/json.h> |
24 | | #include <memory> |
25 | | #include <string> |
26 | | #include <iostream> |
27 | | #include <cstddef> |
28 | | #include <stdint.h> |
29 | | #include <cstring> |
30 | | #include <iostream> |
31 | | |
32 | | namespace Json { |
33 | | class Exception; |
34 | | } |
35 | | |
36 | 2.82k | void FuzzJson(std::string data_str, int32_t hash_settings) { |
37 | 2.82k | Json::CharReaderBuilder builder; |
38 | | |
39 | 2.82k | builder.settings_["failIfExtra"] = hash_settings & (1 << 0); |
40 | 2.82k | builder.settings_["allowComments_"] = hash_settings & (1 << 1); |
41 | 2.82k | builder.settings_["strictRoot_"] = hash_settings & (1 << 2); |
42 | 2.82k | builder.settings_["allowDroppedNullPlaceholders_"] = hash_settings & (1 << 3); |
43 | 2.82k | builder.settings_["allowNumericKeys_"] = hash_settings & (1 << 4); |
44 | 2.82k | builder.settings_["allowSingleQuotes_"] = hash_settings & (1 << 5); |
45 | 2.82k | builder.settings_["failIfExtra_"] = hash_settings & (1 << 6); |
46 | 2.82k | builder.settings_["rejectDupKeys_"] = hash_settings & (1 << 7); |
47 | 2.82k | builder.settings_["allowSpecialFloats_"] = hash_settings & (1 << 8); |
48 | 2.82k | builder.settings_["collectComments"] = hash_settings & (1 << 9); |
49 | 2.82k | builder.settings_["allowTrailingCommas_"] = hash_settings & (1 << 10); |
50 | | |
51 | 2.82k | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
52 | | |
53 | 2.82k | const char* begin = data_str.c_str(); |
54 | 2.82k | const char* end = begin + data_str.length(); |
55 | | |
56 | 2.82k | Json::Value root; |
57 | 2.82k | try { |
58 | 2.82k | reader->parse(begin, end, &root, nullptr); |
59 | 2.82k | } catch (Json::Exception const&) { |
60 | 27 | } |
61 | 2.82k | } |
62 | | |
63 | 2.82k | DEFINE_PROTO_FUZZER(const json_proto::JsonParseAPI &json_proto) { |
64 | 2.82k | json_proto::JsonProtoConverter converter; |
65 | 2.82k | std::string data_str = converter.Convert(json_proto.object_value()); |
66 | 2.82k | int32_t hash_settings = json_proto.settings(); |
67 | 2.82k | FuzzJson(data_str, hash_settings); |
68 | 2.82k | } |