/src/util-linux/libblkid/src/fuzz.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "blkidP.h" |
2 | | #include "fuzz.h" |
3 | | |
4 | | #include <stdlib.h> |
5 | | #include <unistd.h> |
6 | | |
7 | | static int process_file(const char *name) |
8 | 7.28k | { |
9 | 7.28k | int rc = -1; |
10 | 7.28k | blkid_probe pr = blkid_new_probe_from_filename(name); |
11 | 7.28k | if (pr != NULL) { |
12 | 7.28k | blkid_probe_enable_partitions(pr, TRUE); |
13 | 7.28k | blkid_probe_set_partitions_flags(pr, FALSE); |
14 | 7.28k | blkid_probe_enable_superblocks(pr, TRUE); |
15 | 7.28k | blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_DEFAULT | BLKID_SUBLKS_FSINFO | BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_VERSION | BLKID_SUBLKS_BADCSUM); |
16 | 7.28k | rc = blkid_do_safeprobe(pr) == -1 ? -1 : 0; |
17 | 7.28k | } |
18 | 7.28k | blkid_free_probe(pr); |
19 | 7.28k | return rc; |
20 | 7.28k | } |
21 | | |
22 | 7.28k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
23 | 7.28k | int fd; |
24 | 7.28k | char name[] = "/tmp/test-script-fuzz.XXXXXX"; |
25 | | |
26 | 7.28k | fd = mkostemp(name, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC); |
27 | 7.28k | if (fd == -1) |
28 | 0 | err(EXIT_FAILURE, "mkostemp() failed"); |
29 | | |
30 | 7.28k | if (write(fd, data, size) != (ssize_t)size) |
31 | 0 | goto out; |
32 | | |
33 | 7.28k | process_file(name); |
34 | 7.28k | out: |
35 | 7.28k | close(fd); |
36 | 7.28k | unlink(name); |
37 | 7.28k | return 0; |
38 | 7.28k | } |
39 | | |
40 | | #ifndef FUZZ_TARGET |
41 | | int main(int argc, char **argv) |
42 | | { |
43 | | for (int i = 1; i < argc; i++) { |
44 | | printf("%s ", argv[i]); |
45 | | if (process_file(argv[i]) == 0) |
46 | | printf(" OK\n"); |
47 | | else |
48 | | printf(" FAILED\n"); |
49 | | |
50 | | } |
51 | | } |
52 | | #endif |