1
+ #include < assert.h>
2
+ #include < cstdio>
3
+ #include < cstdlib>
4
+ #include < cstring>
5
+ #include < vector>
6
+ #include < cifuzz/cifuzz.h>
7
+ #include < fuzzer/FuzzedDataProvider.h>
8
+
9
+ // RapidJSON includes
10
+ #include " rapidjson/document.h"
11
+ #include " rapidjson/schema.h"
12
+ #include " rapidjson/filereadstream.h"
13
+ #include " rapidjson/error/en.h"
14
+ #include " rapidjson/prettywriter.h"
15
+ #include " rapidjson/stringbuffer.h"
16
+
17
+ // FUZZ_TEST_SETUP:
18
+ // (No one-time setup needed beyond what is shown)
19
+ FUZZ_TEST_SETUP () {
20
+ // One-time initialization tasks can be performed here.
21
+ }
22
+
23
+ FUZZ_TEST (const uint8_t *data, size_t size) {
24
+ // Initialize FuzzedDataProvider (using a single instance)
25
+ FuzzedDataProvider fdp (data, size);
26
+
27
+ // Consume a string for the schema (limit size to 1024)
28
+ std::string schemaStr = fdp.ConsumeRandomLengthString (1024 );
29
+ // Consume remaining data for the JSON instance (limit to 8192)
30
+ std::string jsonStr = fdp.ConsumeRandomLengthString (8192 );
31
+
32
+ // ----------------------------- //
33
+ // Set up the Schema using tmpfile
34
+ // ----------------------------- //
35
+ FILE* schemaFile = tmpfile ();
36
+ if (!schemaFile) return ; // if tmpfile fails, exit
37
+
38
+ // Write schema data to file
39
+ if (!schemaStr.empty ()) {
40
+ fwrite (schemaStr.data (), 1 , schemaStr.size (), schemaFile);
41
+ }
42
+ rewind (schemaFile);
43
+
44
+ // Prepare a buffer, and parse the schema using FileReadStream
45
+ char buffer[4096 ];
46
+ rapidjson::Document d;
47
+ rapidjson::FileReadStream schemaStream (schemaFile, buffer, sizeof (buffer));
48
+ d.ParseStream (schemaStream);
49
+ fclose (schemaFile);
50
+
51
+ // Construct the SchemaDocument from the parsed Document
52
+ rapidjson::SchemaDocument sd (d);
53
+
54
+ // --------------------------------- //
55
+ // Set up JSON instance using tmpfile
56
+ // --------------------------------- //
57
+ FILE* jsonFile = tmpfile ();
58
+ if (!jsonFile) return ;
59
+
60
+ if (!jsonStr.empty ()) {
61
+ fwrite (jsonStr.data (), 1 , jsonStr.size (), jsonFile);
62
+ }
63
+ rewind (jsonFile);
64
+
65
+ // Create a SchemaValidator and Reader.
66
+ rapidjson::SchemaValidator validator (sd);
67
+ rapidjson::Reader reader;
68
+ rapidjson::FileReadStream jsonStream (jsonFile, buffer, sizeof (buffer));
69
+ reader.Parse (jsonStream, validator);
70
+ fclose (jsonFile);
71
+
72
+ // Call additional API functions to further exercise the interface.
73
+ volatile bool valid = validator.IsValid ();
74
+ // When invalid, obtain various API outputs
75
+ if (!valid) {
76
+ rapidjson::GenericStringBuffer<rapidjson::UTF8<>> sb;
77
+ validator.GetInvalidSchemaPointer ().StringifyUriFragment (sb);
78
+ validator.GetInvalidDocumentPointer ().StringifyUriFragment (sb);
79
+ rapidjson::PrettyWriter<rapidjson::GenericStringBuffer<rapidjson::UTF8<>>> writer (sb);
80
+ validator.GetError ().Accept (writer);
81
+ }
82
+ }
0 commit comments