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