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