1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)11 int 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)
37                 assert_se(g = open_memstream_unlocked(&out, &out_size));
38 
39         sd_bus_message_dump(m, g ?: stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER);
40 
41         r = sd_bus_message_rewind(m, true);
42         assert_se(r >= 0);
43 
44         return 0;
45 }
46