/src/valijson/tests/fuzzing/fuzzer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stdexcept> |
2 | | #include <unistd.h> |
3 | | |
4 | | #include <document.h> |
5 | | #include <valijson/adapters/rapidjson_adapter.hpp> |
6 | | #include <valijson/utils/rapidjson_utils.hpp> |
7 | | #include <valijson/schema.hpp> |
8 | | #include <valijson/schema_parser.hpp> |
9 | | |
10 | | using valijson::Schema; |
11 | | using valijson::SchemaParser; |
12 | | using valijson::adapters::RapidJsonAdapter; |
13 | | |
14 | | extern "C" int |
15 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
16 | 105 | { |
17 | 105 | if(size<3) return 0; |
18 | 103 | char input_file[256]; |
19 | 103 | sprintf(input_file, "/tmp/libfuzzer.json"); |
20 | 103 | FILE *fp = fopen(input_file, "wb"); |
21 | 103 | if (!fp) |
22 | 0 | return 0; |
23 | 103 | fwrite(data, size, 1, fp); |
24 | 103 | fclose(fp); |
25 | | |
26 | 103 | rapidjson::Document schemaDocument; |
27 | 103 | if (!valijson::utils::loadDocument(input_file, schemaDocument)) { |
28 | 0 | return 1; |
29 | 0 | } |
30 | | |
31 | 103 | Schema schema; |
32 | 103 | SchemaParser parser; |
33 | 103 | RapidJsonAdapter schemaDocumentAdapter(schemaDocument); |
34 | 103 | try { |
35 | 103 | parser.populateSchema(schemaDocumentAdapter, schema); |
36 | 103 | } catch (std::exception &e) { |
37 | 103 | unlink(input_file); |
38 | 103 | return 1; |
39 | 103 | } |
40 | | |
41 | 0 | unlink(input_file); |
42 | 0 | return 1; |
43 | 103 | } |