summaryrefslogtreecommitdiffstats
path: root/src/fuzz/fuzz-json.c
blob: 3aa9d089e687698d724d63cbfb3021b39ea71724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* SPDX-License-Identifier: LGPL-2.1+ */

#include "alloc-util.h"
#include "fd-util.h"
#include "fuzz.h"
#include "json.h"

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
        _cleanup_free_ char *out = NULL; /* out should be freed after g */
        size_t out_size;
        _cleanup_fclose_ FILE *f = NULL, *g = NULL;
        _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;

        if (size == 0)
                return 0;

        f = fmemopen((char*) data, size, "re");
        assert_se(f);

        if (json_parse_file(f, NULL, &v, NULL, NULL) < 0)
                return 0;

        g = open_memstream(&out, &out_size);
        assert_se(g);

        json_variant_dump(v, 0, g, NULL);
        json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g, NULL);

        return 0;
}