/src/jq/tests/jq_fuzz_parse_stream.c
Line | Count | Source |
1 | | #include <stdint.h> |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | |
5 | | #include "jv.h" |
6 | | |
7 | 3.03k | int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
8 | | // Create null-terminated string |
9 | 3.03k | char *null_terminated = (char *)malloc(size + 1); |
10 | 3.03k | memcpy(null_terminated, (char *)data, size); |
11 | 3.03k | null_terminated[size] = '\0'; |
12 | | |
13 | | // Use the iterative jv_parser API for streaming mode. |
14 | | // The single-shot jv_parse_custom_flags() returns "Unexpected extra JSON |
15 | | // values" for any compound JSON in streaming mode, because the streaming |
16 | | // parser produces multiple tokens but the single-shot API expects exactly |
17 | | // one value. |
18 | 3.03k | jv_parser *parser = jv_parser_new(JV_PARSE_STREAMING); |
19 | 3.03k | jv_parser_set_buf(parser, null_terminated, (int)size, 0); |
20 | | |
21 | 3.03k | jv value; |
22 | 263k | while (jv_is_valid(value = jv_parser_next(parser))) { |
23 | 260k | jv_free(value); |
24 | 260k | } |
25 | 3.03k | jv_free(value); |
26 | | |
27 | 3.03k | jv_parser_free(parser); |
28 | | |
29 | | // Free the null-terminated string |
30 | 3.03k | free(null_terminated); |
31 | | |
32 | 3.03k | return 0; |
33 | 3.03k | } |