/src/flac/oss-fuzz/tool_flac.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* fuzzer_tool_flac |
2 | | * Copyright (C) 2023 Xiph.Org Foundation |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions |
6 | | * are met: |
7 | | * |
8 | | * - Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * |
11 | | * - Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * |
15 | | * - Neither the name of the Xiph.org Foundation nor the names of its |
16 | | * contributors may be used to endorse or promote products derived from |
17 | | * this software without specific prior written permission. |
18 | | * |
19 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
23 | | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
24 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
25 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
26 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
27 | | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
29 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | | */ |
31 | | |
32 | | #include <stdlib.h> |
33 | | #include <string.h> /* for memcpy */ |
34 | | #define FUZZ_TOOL_FLAC |
35 | | #define fprintf(...) |
36 | | #define printf(...) |
37 | | #include "../src/flac/main.c" |
38 | | #include "common.h" |
39 | | |
40 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
41 | | |
42 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
43 | 18.3k | { |
44 | 18.3k | size_t size_left = size; |
45 | 18.3k | size_t arglen; |
46 | 18.3k | char * argv[67]; |
47 | 18.3k | char exename[] = "flac"; |
48 | 18.3k | char filename[] = "/tmp/fuzzXXXXXX"; |
49 | 18.3k | int numarg = 0, maxarg; |
50 | 18.3k | int file_to_fuzz; |
51 | 18.3k | int tmp_stdout, tmp_stdin; |
52 | 18.3k | fpos_t pos_stdout; |
53 | 18.3k | bool use_stdin = false; |
54 | | |
55 | | /* reset global vars */ |
56 | 18.3k | flac__utils_verbosity_ = 0; |
57 | 18.3k | share__opterr = 0; |
58 | 18.3k | share__optind = 0; |
59 | | |
60 | 18.3k | if(size < 2) |
61 | 1 | return 0; |
62 | | |
63 | 18.3k | maxarg = data[0] & 63; |
64 | 18.3k | use_stdin = data[0] & 64; |
65 | 18.3k | size_left--; |
66 | | |
67 | 18.3k | argv[0] = exename; |
68 | 18.3k | numarg++; |
69 | | |
70 | | /* Check whether input is zero delimited */ |
71 | 272k | while((arglen = strnlen((char *)data+(size-size_left),size_left)) < size_left && numarg < maxarg) { |
72 | 254k | argv[numarg++] = (char *)data+(size-size_left); |
73 | 254k | size_left -= arglen + 1; |
74 | 254k | } |
75 | | |
76 | 18.3k | file_to_fuzz = mkstemp(filename); |
77 | | |
78 | 18.3k | if (file_to_fuzz < 0) |
79 | 0 | abort(); |
80 | 18.3k | write(file_to_fuzz,data+(size-size_left),size_left); |
81 | 18.3k | close(file_to_fuzz); |
82 | | |
83 | | /* redirect stdout */ |
84 | 18.3k | fflush(stdout); |
85 | 18.3k | fgetpos(stdout,&pos_stdout); |
86 | 18.3k | tmp_stdout = dup(fileno(stdout)); |
87 | 18.3k | freopen("/dev/null","w",stdout); |
88 | | |
89 | | /* redirect stdin */ |
90 | 18.3k | tmp_stdin = dup(fileno(stdin)); |
91 | | |
92 | 18.3k | if(use_stdin) |
93 | 1.79k | freopen(filename,"r",stdin); |
94 | 16.5k | else { |
95 | 16.5k | freopen("/dev/null","r",stdin); |
96 | 16.5k | argv[numarg++] = filename; |
97 | 16.5k | } |
98 | | |
99 | 18.3k | main_to_fuzz(numarg,argv); |
100 | | |
101 | | /* restore stdout */ |
102 | 18.3k | fflush(stdout); |
103 | 18.3k | dup2(tmp_stdout, fileno(stdout)); |
104 | 18.3k | close(tmp_stdout); |
105 | 18.3k | clearerr(stdout); |
106 | 18.3k | fsetpos(stdout,&pos_stdout); |
107 | | |
108 | | /* restore stdin */ |
109 | 18.3k | dup2(tmp_stdin, fileno(stdin)); |
110 | 18.3k | close(tmp_stdin); |
111 | 18.3k | clearerr(stdin); |
112 | | |
113 | 18.3k | unlink(filename); |
114 | | |
115 | 18.3k | return 0; |
116 | 18.3k | } |
117 | | |