]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fuzz/fuzz-json.c
json: add flags parameter to json_parse_file(), for parsing "sensitive" data
[thirdparty/systemd.git] / src / fuzz / fuzz-json.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "fileio.h"
5 #include "fd-util.h"
6 #include "fuzz.h"
7 #include "json.h"
8
9 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
10 _cleanup_free_ char *out = NULL; /* out should be freed after g */
11 size_t out_size;
12 _cleanup_fclose_ FILE *f = NULL, *g = NULL;
13 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
14
15 if (size == 0)
16 return 0;
17
18 f = fmemopen_unlocked((char*) data, size, "re");
19 assert_se(f);
20
21 if (json_parse_file(f, NULL, 0, &v, NULL, NULL) < 0)
22 return 0;
23
24 g = open_memstream_unlocked(&out, &out_size);
25 assert_se(g);
26
27 json_variant_dump(v, 0, g, NULL);
28 json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g, NULL);
29
30 return 0;
31 }