]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/fuzz.c
Merge branch 'list-columns-options' of https://github.com/masatake/util-linux
[thirdparty/util-linux.git] / libblkid / src / fuzz.c
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 {
9 int rc = -1;
10 blkid_probe pr = blkid_new_probe_from_filename(name);
11 if (pr != NULL) {
12 blkid_probe_enable_partitions(pr, TRUE);
13 blkid_probe_set_partitions_flags(pr, FALSE);
14 blkid_probe_enable_superblocks(pr, TRUE);
15 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_DEFAULT | BLKID_SUBLKS_FSINFO | BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_VERSION | BLKID_SUBLKS_BADCSUM);
16 rc = blkid_do_safeprobe(pr) == -1 ? -1 : 0;
17 }
18 blkid_free_probe(pr);
19 return rc;
20 }
21
22 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
23 int fd;
24 char name[] = "/tmp/test-script-fuzz.XXXXXX";
25
26 fd = mkostemp(name, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
27 if (fd == -1)
28 err(EXIT_FAILURE, "mkostemp() failed");
29
30 if (write(fd, data, size) != (ssize_t)size)
31 goto out;
32
33 process_file(name);
34 out:
35 close(fd);
36 unlink(name);
37 return 0;
38 }
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