/src/glaze/fuzzing/yaml_generic.cpp
Line | Count | Source |
1 | | #include <cstddef> |
2 | | #include <cstdint> |
3 | | #include <glaze/glaze.hpp> |
4 | | #include <glaze/yaml.hpp> |
5 | | #include <vector> |
6 | | |
7 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) |
8 | 39.3k | { |
9 | | // The YAML reader is bounded by `end` and never consults a trailing sentinel, so it must |
10 | | // parse a buffer that has no '\0' after it. An exact-size heap buffer puts the ASAN |
11 | | // redzone immediately after the last byte, so any read past the end fails here. |
12 | 39.3k | { |
13 | 39.3k | const std::vector<char> buffer{Data, Data + Size}; |
14 | | |
15 | 39.3k | static constexpr glz::opts opts{.format = glz::YAML, .null_terminated = false}; |
16 | 39.3k | glz::generic yaml{}; |
17 | 39.3k | auto ec = glz::read<opts>(yaml, buffer); |
18 | 39.3k | if (!ec) { |
19 | 7.60k | [[maybe_unused]] auto s = yaml.size(); |
20 | 7.60k | } |
21 | 39.3k | } |
22 | | |
23 | | // glz::yaml_opts has no null_terminated member at all, so this covers the default |
24 | | // read_yaml path, where check_null_terminated falls back to false. |
25 | 39.3k | { |
26 | 39.3k | const std::vector<char> buffer{Data, Data + Size}; |
27 | | |
28 | 39.3k | glz::generic yaml{}; |
29 | 39.3k | auto ec = glz::read_yaml(yaml, buffer); |
30 | 39.3k | if (!ec) { |
31 | 7.60k | [[maybe_unused]] auto s = yaml.size(); |
32 | 7.60k | } |
33 | 39.3k | } |
34 | | |
35 | | // use a vector with null termination instead of a std::string to avoid |
36 | | // small string optimization to hide bounds problems |
37 | 39.3k | std::vector<char> buffer{Data, Data + Size}; |
38 | 39.3k | buffer.push_back('\0'); |
39 | | |
40 | | // const qualified input buffer |
41 | 39.3k | { |
42 | 39.3k | const auto& input = buffer; |
43 | 39.3k | glz::generic yaml{}; |
44 | 39.3k | auto ec = glz::read_yaml(yaml, input); |
45 | 39.3k | if (!ec) { |
46 | 851 | [[maybe_unused]] auto s = yaml.size(); |
47 | 851 | } |
48 | 39.3k | } |
49 | | |
50 | | // non-const input buffer |
51 | 39.3k | { |
52 | 39.3k | glz::generic yaml{}; |
53 | 39.3k | auto ec = glz::read_yaml(yaml, buffer); |
54 | 39.3k | if (!ec) { |
55 | 851 | [[maybe_unused]] auto s = yaml.size(); |
56 | 851 | } |
57 | 39.3k | } |
58 | | |
59 | 39.3k | return 0; |
60 | 39.3k | } |