Coverage Report

Created: 2019-06-19 13:33

/src/systemd/src/fuzz/fuzz-bus-message.c
Line
Count
Source
1
/* SPDX-License-Identifier: LGPL-2.1+ */
2
3
#include "alloc-util.h"
4
#include "bus-dump.h"
5
#include "bus-message.h"
6
#include "env-util.h"
7
#include "fd-util.h"
8
#include "fileio.h"
9
#include "fuzz.h"
10
11
4.27k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
12
4.27k
        _cleanup_free_ char *out = NULL; /* out should be freed after g */
13
4.27k
        size_t out_size;
14
4.27k
        _cleanup_fclose_ FILE *g = NULL;
15
4.27k
        _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
16
4.27k
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
17
4.27k
        _cleanup_free_ void *buffer = NULL;
18
4.27k
        int r;
19
4.27k
20
4.27k
        /* We don't want to fill the logs with messages about parse errors.
21
4.27k
         * Disable most logging if not running standalone */
22
4.27k
        if (!getenv("SYSTEMD_LOG_LEVEL"))
23
4.27k
                log_set_max_level(LOG_CRIT);
24
4.27k
25
4.27k
        r = sd_bus_new(&bus);
26
4.27k
        assert_se(r >= 0);
27
4.27k
28
4.27k
        assert_se(buffer = memdup(data, size));
29
4.27k
30
4.27k
        r = bus_message_from_malloc(bus, buffer, size, NULL, 0, NULL, &m);
31
4.27k
        if (r == -EBADMSG)
32
4.27k
                return 0;
33
2.38k
        assert_se(r >= 0);
34
2.38k
        TAKE_PTR(buffer);
35
2.38k
36
2.38k
        if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
37
2.38k
                assert_se(g = open_memstream_unlocked(&out, &out_size));
38
2.38k
39
2.38k
        bus_message_dump(m, g ?: stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
40
2.38k
41
2.38k
        r = sd_bus_message_rewind(m, true);
42
2.38k
        assert_se(r >= 0);
43
2.38k
44
2.38k
        return 0;
45
2.38k
}