/src/glaze/fuzzing/json_with_comments.cpp
Line | Count | Source |
1 | | #include <array> |
2 | | #include <cstddef> |
3 | | #include <cstdint> |
4 | | #include <cstdlib> |
5 | | #include <glaze/glaze.hpp> |
6 | | #include <string> |
7 | | #include <vector> |
8 | | |
9 | | struct my_struct |
10 | | { |
11 | | int i = 287; |
12 | | double d = 3.14; |
13 | | std::string hello = "Hello World"; |
14 | | std::array<uint64_t, 3> arr = {1, 2, 3}; |
15 | | }; |
16 | | |
17 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) |
18 | 17.8k | { |
19 | | // use a vector with null termination instead of a std::string to avoid |
20 | | // small string optimization to hide bounds problems |
21 | 17.8k | std::vector<char> buffer{Data, Data + Size}; |
22 | 17.8k | buffer.push_back('\0'); |
23 | | |
24 | 17.8k | [[maybe_unused]] auto s = glz::read_jsonc<my_struct>(std::string_view{buffer.data(), Size}); |
25 | 17.8k | if (s) { |
26 | | // hooray! valid json found |
27 | 447 | } |
28 | | |
29 | | // A valid document has to survive the formatters unchanged. Two independent comment scanners run |
30 | | // here -- skip_comment on the read path and read_jsonc_comment in the writers -- and a |
31 | | // disagreement between them is invisible to either side on its own: the reader accepted comment |
32 | | // shapes the writer truncated, and the writer emitted shapes the reader could not read back. |
33 | | // |
34 | | // Gated on validate_jsonc rather than on the read, because a read stops at the first complete |
35 | | // value and says nothing about what trails it. Nothing is promised about a document that does not |
36 | | // validate: minifying is free to run tokens together that only whitespace separated, which is |
37 | | // what dropping a line comment does to "8 // c" followed by "6". |
38 | 17.8k | { |
39 | 17.8k | std::vector<char> read_buffer{Data, Data + Size}; |
40 | 17.8k | read_buffer.push_back('\0'); |
41 | 17.8k | const std::string_view document{read_buffer.data(), Size}; |
42 | 17.8k | glz::generic value{}; |
43 | 17.8k | if (not glz::validate_jsonc(document) && not glz::read_jsonc(value, document)) { |
44 | 9.97k | std::string original{}; |
45 | 9.97k | if (not glz::write_json(value, original)) { |
46 | 19.9k | auto round_trips = [&](const std::string& formatted) { |
47 | 19.9k | glz::generic reparsed{}; |
48 | 19.9k | if (glz::read_jsonc(reparsed, formatted)) { |
49 | 0 | return false; |
50 | 0 | } |
51 | 19.9k | std::string rewritten{}; |
52 | 19.9k | if (glz::write_json(reparsed, rewritten)) { |
53 | 0 | return false; |
54 | 0 | } |
55 | 19.9k | return rewritten == original; |
56 | 19.9k | }; |
57 | | |
58 | | // A formatter that rejects a document the validator accepted is the same disagreement |
59 | | // as one that mangles it, so an error here is a failure rather than a reason to skip |
60 | | // the check. Gating on success would hide exactly the regression this guards against. |
61 | 9.97k | std::string minify_in{Data, Data + Size}; |
62 | 9.97k | std::string minified{}; |
63 | 9.97k | if (glz::minify_jsonc(minify_in, minified) || not round_trips(minified)) { |
64 | 0 | std::abort(); // not assert: a fuzz target has to fire under NDEBUG too |
65 | 0 | } |
66 | | |
67 | 9.97k | const std::string prettify_in{Data, Data + Size}; |
68 | 9.97k | std::string prettified{}; |
69 | 9.97k | if (glz::prettify_jsonc(prettify_in, prettified) || not round_trips(prettified)) { |
70 | 0 | std::abort(); // not assert: a fuzz target has to fire under NDEBUG too |
71 | 0 | } |
72 | 9.97k | } |
73 | 9.97k | } |
74 | 17.8k | } |
75 | | |
76 | 17.8k | return 0; |
77 | 17.8k | } |