/src/glaze/fuzzing/jsonb_reflection.cpp
Line | Count | Source |
1 | | #include <array> |
2 | | #include <cstddef> |
3 | | #include <cstdint> |
4 | | #include <glaze/glaze.hpp> |
5 | | #include <glaze/jsonb.hpp> |
6 | | #include <map> |
7 | | #include <optional> |
8 | | #include <string> |
9 | | #include <variant> |
10 | | #include <vector> |
11 | | |
12 | | struct nested |
13 | | { |
14 | | int n = 0; |
15 | | std::string label{}; |
16 | | std::vector<double> data{}; |
17 | | }; |
18 | | |
19 | | struct my_struct |
20 | | { |
21 | | int i = 287; |
22 | | double d = 3.14; |
23 | | std::string hello = "Hello World"; |
24 | | std::array<uint64_t, 3> arr = {1, 2, 3}; |
25 | | std::optional<std::string> maybe{}; |
26 | | std::map<std::string, int> dict{}; |
27 | | std::vector<nested> kids{}; |
28 | | std::variant<int, std::string, double> alt{}; |
29 | | }; |
30 | | |
31 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) |
32 | 36.5k | { |
33 | | // Use a vector with explicit bounds rather than std::string to avoid SSO masking |
34 | | // bounds-check bugs. |
35 | 36.5k | std::vector<char> buffer{Data, Data + Size}; |
36 | 36.5k | const auto& input = buffer; |
37 | | |
38 | | // 1. Drive the typed reader: exercises header parsing, type dispatch, container |
39 | | // traversal, hash key lookup, depth guard, INT bounds checking. |
40 | 36.5k | { |
41 | 36.5k | [[maybe_unused]] auto s = glz::read_jsonb<my_struct>(input); |
42 | 36.5k | } |
43 | | |
44 | | // 2. Drive the JSONB→JSON converter: separate code path with its own depth |
45 | | // tracking and TEXT/TEXTRAW/TEXTJ/TEXT5 emission paths. |
46 | 36.5k | { |
47 | 36.5k | std::string json_output{}; |
48 | 36.5k | [[maybe_unused]] auto ec = glz::jsonb_to_json(input, json_output); |
49 | 36.5k | } |
50 | | |
51 | | // 3. Drive the schemaless reader (glz::generic) — covers paths the typed reader |
52 | | // short-circuits on type mismatch. |
53 | 36.5k | { |
54 | 36.5k | [[maybe_unused]] auto s = glz::read_jsonb<glz::generic>(input); |
55 | 36.5k | } |
56 | | |
57 | 36.5k | return 0; |
58 | 36.5k | } |