/src/jansson/test/ossfuzz/json_equal_copy_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * json_equal_copy_fuzzer.cc |
3 | | * |
4 | | * Fuzz target for jansson equality and copying APIs. |
5 | | * The existing json_load_dump_fuzzer covers json_loads/json_dumps only. |
6 | | * This harness exercises the following uncovered paths: |
7 | | * |
8 | | * json_loadb() - length-bounded load (no NUL required) |
9 | | * json_equal() - deep equality comparison |
10 | | * json_copy() - shallow copy |
11 | | * json_deep_copy() - recursive deep copy |
12 | | * json_object_update() - merge two objects |
13 | | * json_object_update_existing() - update only existing keys |
14 | | */ |
15 | | |
16 | | #include <stddef.h> |
17 | | #include <stdint.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include "jansson.h" |
22 | | |
23 | 5.28k | static uint8_t u8(const uint8_t **d, size_t *s) { |
24 | 5.28k | if (*s == 0) |
25 | 0 | return 0; |
26 | 5.28k | uint8_t v = **d; |
27 | 5.28k | (*d)++; |
28 | 5.28k | (*s)--; |
29 | 5.28k | return v; |
30 | 5.28k | } |
31 | | |
32 | 2.64k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
33 | 2.64k | if (size < 2) |
34 | 1 | return 0; |
35 | | |
36 | 2.64k | uint8_t op = u8(&data, &size); |
37 | 2.64k | uint8_t flag_byte = u8(&data, &size); |
38 | | |
39 | 2.64k | size_t load_flags = 0; |
40 | 2.64k | if (flag_byte & 0x01) |
41 | 1.49k | load_flags |= JSON_REJECT_DUPLICATES; |
42 | 2.64k | if (flag_byte & 0x02) |
43 | 1.39k | load_flags |= JSON_DECODE_ANY; |
44 | 2.64k | if (flag_byte & 0x04) |
45 | 1.35k | load_flags |= JSON_DISABLE_EOF_CHECK; |
46 | 2.64k | if (flag_byte & 0x08) |
47 | 1.39k | load_flags |= JSON_DECODE_INT_AS_REAL; |
48 | 2.64k | if (flag_byte & 0x10) |
49 | 1.42k | load_flags |= JSON_ALLOW_NUL; |
50 | | |
51 | | /* Parse with json_loadb — does not require NUL termination */ |
52 | 2.64k | json_error_t err; |
53 | 2.64k | json_t *root = json_loadb((const char *)data, size, load_flags, &err); |
54 | 2.64k | if (!root) |
55 | 1.80k | return 0; |
56 | | |
57 | 840 | switch (op % 5) { |
58 | 283 | case 0: { |
59 | | /* Deep copy then compare for equality */ |
60 | 283 | json_t *copy = json_deep_copy(root); |
61 | 283 | if (copy) { |
62 | 283 | json_equal(root, copy); |
63 | 283 | json_decref(copy); |
64 | 283 | } |
65 | 283 | break; |
66 | 0 | } |
67 | 139 | case 1: { |
68 | | /* Shallow copy */ |
69 | 139 | json_t *shallow = json_copy(root); |
70 | 139 | if (shallow) { |
71 | 139 | json_equal(root, shallow); |
72 | 139 | json_decref(shallow); |
73 | 139 | } |
74 | 139 | break; |
75 | 0 | } |
76 | 19 | case 2: { |
77 | | /* Compare root with itself */ |
78 | 19 | json_equal(root, root); |
79 | 19 | break; |
80 | 0 | } |
81 | 62 | case 3: { |
82 | | /* json_object_update: merge root into a new empty object */ |
83 | 62 | if (json_is_object(root)) { |
84 | 28 | json_t *target = json_object(); |
85 | 28 | if (target) { |
86 | 28 | json_object_update(target, root); |
87 | 28 | json_decref(target); |
88 | 28 | } |
89 | 28 | } |
90 | 62 | break; |
91 | 0 | } |
92 | 337 | case 4: { |
93 | | /* json_object_update_existing: only update keys present in target */ |
94 | 337 | if (json_is_object(root)) { |
95 | 293 | json_t *target = json_deep_copy(root); |
96 | 293 | if (target) { |
97 | 293 | json_t *patch = json_deep_copy(root); |
98 | 293 | if (patch) { |
99 | 293 | json_object_update_existing(target, patch); |
100 | 293 | json_decref(patch); |
101 | 293 | } |
102 | 293 | json_decref(target); |
103 | 293 | } |
104 | 293 | } |
105 | 337 | break; |
106 | 0 | } |
107 | 840 | } |
108 | | |
109 | 840 | json_decref(root); |
110 | 840 | return 0; |
111 | 840 | } |