]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fuzz/fuzz-bus-message.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / fuzz / fuzz-bus-message.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
56b560c2 2
56b560c2
ZJS
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"
673a1e6f 8#include "fileio.h"
56b560c2
ZJS
9#include "fuzz.h"
10
11int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
12 _cleanup_free_ char *out = NULL; /* out should be freed after g */
13 size_t out_size;
14 _cleanup_fclose_ FILE *g = NULL;
15 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
16 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
17 _cleanup_free_ void *buffer = NULL;
18 int r;
19
20 /* We don't want to fill the logs with messages about parse errors.
21 * Disable most logging if not running standalone */
22 if (!getenv("SYSTEMD_LOG_LEVEL"))
23 log_set_max_level(LOG_CRIT);
24
25 r = sd_bus_new(&bus);
26 assert_se(r >= 0);
27
28 assert_se(buffer = memdup(data, size));
29
30 r = bus_message_from_malloc(bus, buffer, size, NULL, 0, NULL, &m);
31 if (r == -EBADMSG)
32 return 0;
33 assert_se(r >= 0);
34 TAKE_PTR(buffer);
35
36 if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
673a1e6f 37 assert_se(g = open_memstream_unlocked(&out, &out_size));
56b560c2 38
2b4a65b6 39 sd_bus_message_dump(m, g ?: stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER);
56b560c2
ZJS
40
41 r = sd_bus_message_rewind(m, true);
42 assert_se(r >= 0);
43
44 return 0;
45}