Coverage Report

Created: 2026-08-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flac/oss-fuzz/tool_metaflac.c
Line
Count
Source
1
/* fuzzer_tool_flac
2
 * Copyright (C) 2023-2025  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 <stdio.h>
33
#include <stdlib.h>
34
#include <string.h> /* for memcpy */
35
#define FUZZ_TOOL_METAFLAC
36
#define fprintf(...)
37
#define printf(...)
38
#include "../src/metaflac/main.c"
39
#include "common.h"
40
41
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
42
43
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
44
15.8k
{
45
15.8k
  size_t size_left = size;
46
15.8k
  size_t arglen;
47
15.8k
  char * argv[64];
48
15.8k
  char exename[] = "metaflac";
49
15.8k
  char filename[] = "/tmp/fuzzXXXXXX";
50
15.8k
  char filename_stdin[] = "/tmp/fuzzXXXXXX";
51
15.8k
  int numarg = 0, maxarg;
52
15.8k
  int file_to_fuzz;
53
15.8k
  int tmp_stdout, tmp_stdin;
54
15.8k
  fpos_t pos_stdout;
55
15.8k
  bool use_stdin = false;
56
57
15.8k
  share__opterr = 0;
58
15.8k
  share__optind = 0;
59
60
15.8k
  allowed_filename = NULL;
61
62
63
15.8k
  if(size < 2)
64
1
    return 0;
65
66
15.8k
  maxarg = data[0] & 15;
67
15.8k
  use_stdin = data[0] & 16;
68
15.8k
  size_left--;
69
70
15.8k
  argv[0] = exename;
71
15.8k
  numarg++;
72
73
  /* Check whether input is zero delimited */
74
64.2k
  while((arglen = strnlen((char *)data+(size-size_left),size_left)) < size_left && numarg < maxarg) {
75
48.3k
    argv[numarg++] = (char *)data+(size-size_left);
76
48.3k
    size_left -= arglen + 1;
77
48.3k
  }
78
79
  /* Create file to feed directly */
80
15.8k
  file_to_fuzz = mkstemp(filename);
81
15.8k
  if (file_to_fuzz < 0)
82
0
    abort();
83
15.8k
  if(use_stdin) {
84
1.09k
    write(file_to_fuzz,data+(size-size_left),size_left/2);
85
1.09k
    size_left -= size_left/2;
86
1.09k
  }
87
14.7k
  else
88
14.7k
    write(file_to_fuzz,data+(size-size_left),size_left);
89
15.8k
  close(file_to_fuzz);
90
91
15.8k
  argv[numarg++] = filename;
92
93
15.8k
  allowed_filename = filename;
94
95
  /* Create file to feed to stdin */
96
15.8k
  if(use_stdin) {
97
1.09k
    file_to_fuzz = mkstemp(filename_stdin);
98
1.09k
    if (file_to_fuzz < 0)
99
0
      abort();
100
1.09k
    write(file_to_fuzz,data+(size-size_left),size_left);
101
1.09k
    close(file_to_fuzz);
102
1.09k
  }
103
104
  /* redirect stdout */
105
15.8k
  fflush(stdout);
106
15.8k
  fgetpos(stdout,&pos_stdout);
107
15.8k
  tmp_stdout = dup(fileno(stdout));
108
15.8k
  freopen("/dev/null","w",stdout);
109
110
  /* redirect stdin */
111
15.8k
  tmp_stdin = dup(fileno(stdin));
112
15.8k
  if(use_stdin)
113
1.09k
    freopen(filename_stdin,"r",stdin);
114
14.7k
  else {
115
14.7k
    freopen("/dev/null","r",stdin);
116
14.7k
    argv[numarg++] = filename;
117
14.7k
  }
118
119
15.8k
  main_to_fuzz(numarg,argv);
120
121
  /* restore stdout */
122
15.8k
  fflush(stdout);
123
15.8k
  dup2(tmp_stdout, fileno(stdout));
124
15.8k
  close(tmp_stdout);
125
15.8k
  clearerr(stdout);
126
15.8k
  fsetpos(stdout,&pos_stdout);
127
128
  /* restore stdin */
129
15.8k
  dup2(tmp_stdin, fileno(stdin));
130
15.8k
  close(tmp_stdin);
131
15.8k
  clearerr(stdin);
132
133
15.8k
  unlink(filename);
134
135
15.8k
  if(use_stdin)
136
1.09k
    unlink(filename_stdin);
137
138
15.8k
  return 0;
139
15.8k
}
140