Coverage Report

Created: 2026-08-13 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsndfile/ossfuzz/sndfile_fuzz_header.h
Line
Count
Source
1
#ifndef SNDFILE_FUZZ_HEADER_H
2
#define SNDFILE_FUZZ_HEADER_H
3
4
#include <errno.h>
5
6
typedef struct
7
{
8
  sf_count_t offset ;
9
  sf_count_t length ;
10
  const unsigned char *data ;
11
} VIO_DATA ;
12
13
static sf_count_t vfget_filelen (void *user_data)
14
42.3k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
42.3k
   return vf->length ;
16
42.3k
}
sndfile_alt_fuzzer.cc:vfget_filelen(void*)
Line
Count
Source
14
23.9k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
23.9k
   return vf->length ;
16
23.9k
}
sndfile_fuzzer.cc:vfget_filelen(void*)
Line
Count
Source
14
18.3k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
18.3k
   return vf->length ;
16
18.3k
}
17
18
static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data)
19
4.07M
{
20
4.07M
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
4.07M
  sf_count_t new_offset ;
22
23
4.07M
  switch (whence)
24
4.07M
  {   case SEEK_SET :
25
107k
        new_offset = offset ;
26
107k
        break ;
27
28
3.96M
    case SEEK_CUR :
29
3.96M
        new_offset = vf->offset + offset ;
30
3.96M
        break ;
31
32
0
    case SEEK_END :
33
0
        new_offset = vf->length + offset ;
34
0
        break ;
35
36
0
    default :
37
        // SEEK_DATA and SEEK_HOLE are not supported by this function.
38
0
        errno = EINVAL ;
39
0
        return -1 ;
40
0
        break ;
41
4.07M
  }
42
43
  /* Ensure you can't seek outside the data */
44
4.07M
  if (new_offset > vf->length)
45
5.93k
  {  /* Trying to seek past the end of the data */
46
5.93k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
5.93k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
5.93k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
5.93k
     new_offset = vf->length ;
50
5.93k
  }
51
4.06M
  else if (new_offset < 0)
52
536k
  {  /* Trying to seek before the start of the data */
53
536k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
536k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
536k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
536k
     new_offset = 0 ;
57
536k
  }
58
4.07M
  vf->offset = new_offset ;
59
60
4.07M
  return vf->offset ;
61
4.07M
}
sndfile_alt_fuzzer.cc:vfseek(long, int, void*)
Line
Count
Source
19
968k
{
20
968k
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
968k
  sf_count_t new_offset ;
22
23
968k
  switch (whence)
24
968k
  {   case SEEK_SET :
25
70.2k
        new_offset = offset ;
26
70.2k
        break ;
27
28
897k
    case SEEK_CUR :
29
897k
        new_offset = vf->offset + offset ;
30
897k
        break ;
31
32
0
    case SEEK_END :
33
0
        new_offset = vf->length + offset ;
34
0
        break ;
35
36
0
    default :
37
        // SEEK_DATA and SEEK_HOLE are not supported by this function.
38
0
        errno = EINVAL ;
39
0
        return -1 ;
40
0
        break ;
41
968k
  }
42
43
  /* Ensure you can't seek outside the data */
44
968k
  if (new_offset > vf->length)
45
3.18k
  {  /* Trying to seek past the end of the data */
46
3.18k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
3.18k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
3.18k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
3.18k
     new_offset = vf->length ;
50
3.18k
  }
51
965k
  else if (new_offset < 0)
52
308k
  {  /* Trying to seek before the start of the data */
53
308k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
308k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
308k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
308k
     new_offset = 0 ;
57
308k
  }
58
968k
  vf->offset = new_offset ;
59
60
968k
  return vf->offset ;
61
968k
}
sndfile_fuzzer.cc:vfseek(long, int, void*)
Line
Count
Source
19
3.10M
{
20
3.10M
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
3.10M
  sf_count_t new_offset ;
22
23
3.10M
  switch (whence)
24
3.10M
  {   case SEEK_SET :
25
37.2k
        new_offset = offset ;
26
37.2k
        break ;
27
28
3.06M
    case SEEK_CUR :
29
3.06M
        new_offset = vf->offset + offset ;
30
3.06M
        break ;
31
32
0
    case SEEK_END :
33
0
        new_offset = vf->length + offset ;
34
0
        break ;
35
36
0
    default :
37
        // SEEK_DATA and SEEK_HOLE are not supported by this function.
38
0
        errno = EINVAL ;
39
0
        return -1 ;
40
0
        break ;
41
3.10M
  }
42
43
  /* Ensure you can't seek outside the data */
44
3.10M
  if (new_offset > vf->length)
45
2.75k
  {  /* Trying to seek past the end of the data */
46
2.75k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
2.75k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
2.75k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
2.75k
     new_offset = vf->length ;
50
2.75k
  }
51
3.10M
  else if (new_offset < 0)
52
228k
  {  /* Trying to seek before the start of the data */
53
228k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
228k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
228k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
228k
     new_offset = 0 ;
57
228k
  }
58
3.10M
  vf->offset = new_offset ;
59
60
3.10M
  return vf->offset ;
61
3.10M
}
62
63
static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data)
64
30.0M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
30.0M
   if (vf->offset + count > vf->length)
67
16.6M
     count = vf->length - vf->offset ;
68
69
30.0M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
30.0M
   vf->offset += count ;
71
72
30.0M
   return count ;
73
30.0M
}
sndfile_alt_fuzzer.cc:vfread(void*, long, void*)
Line
Count
Source
64
17.4M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
17.4M
   if (vf->offset + count > vf->length)
67
9.92M
     count = vf->length - vf->offset ;
68
69
17.4M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
17.4M
   vf->offset += count ;
71
72
17.4M
   return count ;
73
17.4M
}
sndfile_fuzzer.cc:vfread(void*, long, void*)
Line
Count
Source
64
12.6M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
12.6M
   if (vf->offset + count > vf->length)
67
6.76M
     count = vf->length - vf->offset ;
68
69
12.6M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
12.6M
   vf->offset += count ;
71
72
12.6M
   return count ;
73
12.6M
}
74
75
static sf_count_t vfwrite (const void *ptr, sf_count_t count, void *user_data)
76
0
{
77
0
  (void)ptr ;
78
0
  (void)count ;
79
0
  (void)user_data ;
80
81
  // Cannot write to this virtual file.
82
0
  return 0;
83
0
}
Unexecuted instantiation: sndfile_alt_fuzzer.cc:vfwrite(void const*, long, void*)
Unexecuted instantiation: sndfile_fuzzer.cc:vfwrite(void const*, long, void*)
84
85
static sf_count_t vftell (void *user_data)
86
32.3M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
32.3M
  return vf->offset ;
89
32.3M
}
sndfile_alt_fuzzer.cc:vftell(void*)
Line
Count
Source
86
8.26M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
8.26M
  return vf->offset ;
89
8.26M
}
sndfile_fuzzer.cc:vftell(void*)
Line
Count
Source
86
24.0M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
24.0M
  return vf->offset ;
89
24.0M
}
90
91
int sf_init_file(const uint8_t *data, 
92
                size_t size, 
93
                SNDFILE **sndfile, 
94
                VIO_DATA *vio_data, 
95
                SF_VIRTUAL_IO *vio, SF_INFO *sndfile_info)
96
38.5k
{
97
   // Initialize the virtual IO structure.
98
38.5k
   vio->get_filelen = vfget_filelen ;
99
38.5k
   vio->seek = vfseek ;
100
38.5k
   vio->read = vfread ;
101
38.5k
   vio->write = vfwrite ;
102
38.5k
   vio->tell = vftell ;
103
104
   // Initialize the VIO user data.
105
38.5k
   vio_data->data = data ;
106
38.5k
   vio_data->length = size ;
107
38.5k
   vio_data->offset = 0 ;
108
109
38.5k
   memset(sndfile_info, 0, sizeof(SF_INFO)) ;
110
111
   // Try and open the virtual file.
112
38.5k
   *sndfile = sf_open_virtual(vio, SFM_READ, sndfile_info, vio_data) ;
113
114
38.5k
   if (sndfile_info->channels == 0)
115
30.5k
     return -1 ;
116
117
8.07k
   if (sndfile_info->channels > 1024 * 1024)
118
0
     return -1 ;
119
120
8.07k
   return 0;
121
8.07k
}
122
123
#endif