/src/libcue/oss-fuzz/fuzz.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stdint.h> |
2 | | #include <stddef.h> |
3 | | #include <stdlib.h> |
4 | | #include <string.h> |
5 | | #include "libcue.h" |
6 | | |
7 | | extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size); |
8 | | |
9 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
10 | | |
11 | | /* |
12 | | ** Read files named on the command-line and invoke the fuzzer harness for |
13 | | ** each one. |
14 | | */ |
15 | | int main (int argc, char **argv) |
16 | | { |
17 | | FILE *in; |
18 | | int i; |
19 | | int nErr = 0; |
20 | | uint8_t *zBuf = 0; |
21 | | size_t sz; |
22 | | |
23 | | for (i = 1; i < argc; i++) |
24 | | { |
25 | | const char *zFilename = argv[i]; |
26 | | in = fopen (zFilename, "rb"); |
27 | | if (in == 0) |
28 | | { |
29 | | fprintf (stderr, "cannot open \"%s\"\n", zFilename); |
30 | | nErr++; |
31 | | continue; |
32 | | } |
33 | | fseek (in, 0, SEEK_END); |
34 | | sz = ftell (in); |
35 | | rewind (in); |
36 | | zBuf = (uint8_t*)realloc (zBuf, sz); |
37 | | if (zBuf == 0) |
38 | | { |
39 | | fprintf(stderr, "cannot malloc() for %d bytes\n", (int)sz); |
40 | | exit(1); |
41 | | } |
42 | | if (fread (zBuf, sz, 1, in) != 1) |
43 | | { |
44 | | fprintf (stderr, "cannot read %d bytes from \"%s\"\n", |
45 | | (int)sz, zFilename); |
46 | | nErr++; |
47 | | } |
48 | | else |
49 | | { |
50 | | printf ("%s... ", zFilename); |
51 | | fflush (stdout); |
52 | | (void)LLVMFuzzerTestOneInput (zBuf, sz); |
53 | | printf ("ok\n"); |
54 | | } |
55 | | fclose (in); |
56 | | } |
57 | | free (zBuf); |
58 | | return nErr; |
59 | | } |
60 | | |
61 | | #endif |
62 | | |
63 | | extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) |
64 | 1.90k | { |
65 | 1.90k | char* str = (char*)malloc (size + 1); |
66 | | |
67 | 1.90k | if (!str) |
68 | 0 | return -1; |
69 | | |
70 | 1.90k | memcpy (str, data, size); |
71 | 1.90k | str[size] = '\0'; |
72 | | |
73 | 1.90k | Cd *cd = cue_parse_string (str); |
74 | 1.90k | cd_delete (cd); |
75 | | |
76 | 1.90k | free (str); |
77 | 1.90k | return 0; |
78 | 1.90k | } |