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