/src/qpdf/fuzz/json_fuzzer.cc
Line | Count | Source |
1 | | #include "qpdf/JSON.hh" |
2 | | #include "qpdf/QPDF.hh" |
3 | | #include <qpdf/BufferInputSource.hh> |
4 | | #include <qpdf/global.hh> |
5 | | |
6 | | #include <iostream> |
7 | | #include <stdexcept> |
8 | | |
9 | | class FuzzHelper |
10 | | { |
11 | | public: |
12 | | FuzzHelper(unsigned char const* data, size_t size) : |
13 | 7.90k | data(data), |
14 | 7.90k | size(size) |
15 | 7.90k | { |
16 | 7.90k | } |
17 | | |
18 | | void |
19 | | run() |
20 | 7.90k | { |
21 | 7.90k | qpdf::global::options::fuzz_mode(true); |
22 | 7.90k | try { |
23 | 7.90k | JSON::parse(std::string(reinterpret_cast<char const*>(data), size)); |
24 | 7.90k | } catch (std::runtime_error& e) { |
25 | 7.76k | std::cerr << "runtime_error parsing json: " << e.what() << '\n'; |
26 | 7.76k | } |
27 | | |
28 | 7.90k | QPDF q; |
29 | 7.90k | Buffer buf(const_cast<unsigned char*>(data), size); |
30 | 7.90k | auto is = std::make_shared<BufferInputSource>("json", &buf); |
31 | 7.90k | try { |
32 | 7.90k | q.createFromJSON(is); |
33 | 7.90k | } catch (std::runtime_error const& e) { |
34 | 7.89k | std::cerr << "runtime_error: " << e.what() << '\n'; |
35 | 7.89k | } |
36 | 7.90k | } |
37 | | |
38 | | private: |
39 | | unsigned char const* data; |
40 | | size_t size; |
41 | | }; |
42 | | |
43 | | extern "C" int |
44 | | LLVMFuzzerTestOneInput(unsigned char const* data, size_t size) |
45 | 7.90k | { |
46 | 7.90k | FuzzHelper f(data, size); |
47 | 7.90k | f.run(); |
48 | 7.90k | return 0; |
49 | 7.90k | } |