/src/crow/tests/fuzz/json_fuzzer.cpp
Line | Count | Source |
1 | | #include <cstdint> |
2 | | #include <string> |
3 | | #include "crow/json.h" |
4 | | |
5 | 4.23k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
6 | 4.23k | if (size == 0) { |
7 | 0 | return 0; |
8 | 0 | } |
9 | | |
10 | 4.23k | std::string input(reinterpret_cast<const char*>(data), size); |
11 | 4.23k | try { |
12 | 4.23k | crow::json::rvalue json_val = crow::json::load(input); |
13 | 4.23k | if (json_val) { |
14 | | // If parsing succeeded, try to access some properties to trigger more code |
15 | 2.70k | auto t = json_val.t(); |
16 | 2.70k | if (t == crow::json::type::Object) { |
17 | 3.77M | for (const auto& key : json_val.keys()) { |
18 | 3.77M | auto& val = json_val[key]; |
19 | 3.77M | (void)val.t(); |
20 | 3.77M | } |
21 | 2.61k | } else if (t == crow::json::type::List) { |
22 | 211 | for (const auto& val : json_val) { |
23 | 211 | (void)val.t(); |
24 | 211 | } |
25 | 18 | } |
26 | | |
27 | | // Also try to dump it back to string |
28 | | // (void)crow::json::dump(json_val); // Need to check if dump exists |
29 | 2.70k | } |
30 | 4.23k | } catch (...) { |
31 | | // Ignore exceptions from invalid JSON |
32 | 0 | } |
33 | | |
34 | 4.23k | return 0; |
35 | 4.23k | } |