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