1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <stdio.h>
4 
5 #include "fd-util.h"
6 #include "fs-util.h"
7 #include "fuzz.h"
8 #include "tests.h"
9 #include "tmpfile-util.h"
10 #include "udev-rules.h"
11 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)12 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
13         _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
14         _cleanup_fclose_ FILE *f = NULL;
15         _cleanup_(unlink_tempfilep) char filename[] = "/tmp/fuzz-udev-rules.XXXXXX";
16         int r;
17 
18         if (outside_size_range(size, 0, 65536))
19                 return 0;
20 
21         if (!getenv("SYSTEMD_LOG_LEVEL"))
22                 log_set_max_level(LOG_CRIT);
23 
24         assert_se(fmkostemp_safe(filename, "r+", &f) == 0);
25         if (size != 0)
26                 assert_se(fwrite(data, size, 1, f) == 1);
27         fflush(f);
28 
29         assert_se(rules = udev_rules_new(RESOLVE_NAME_EARLY));
30         r = udev_rules_parse_file(rules, filename);
31         log_info_errno(r, "Parsing %s: %m", filename);
32         assert_se(IN_SET(r,
33                          0,       /* OK */
34                          -ENOBUFS /* line length exceeded */));
35 
36         return 0;
37 }
38