Coverage Report

Created: 2026-02-26 06:48

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
32.3k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
32.3k
   return vf->length ;
16
32.3k
}
sndfile_alt_fuzzer.cc:vfget_filelen(void*)
Line
Count
Source
14
18.6k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
18.6k
   return vf->length ;
16
18.6k
}
sndfile_fuzzer.cc:vfget_filelen(void*)
Line
Count
Source
14
13.6k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
13.6k
   return vf->length ;
16
13.6k
}
17
18
static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data)
19
817k
{
20
817k
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
817k
  sf_count_t new_offset ;
22
23
817k
  switch (whence)
24
817k
  {   case SEEK_SET :
25
104k
        new_offset = offset ;
26
104k
        break ;
27
28
712k
    case SEEK_CUR :
29
712k
        new_offset = vf->offset + offset ;
30
712k
        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
817k
  }
42
43
  /* Ensure you can't seek outside the data */
44
817k
  if (new_offset > vf->length)
45
4.94k
  {  /* Trying to seek past the end of the data */
46
4.94k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
4.94k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
4.94k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
4.94k
     new_offset = vf->length ;
50
4.94k
  }
51
812k
  else if (new_offset < 0)
52
475k
  {  /* Trying to seek before the start of the data */
53
475k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
475k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
475k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
475k
     new_offset = 0 ;
57
475k
  }
58
817k
  vf->offset = new_offset ;
59
60
817k
  return vf->offset ;
61
817k
}
sndfile_alt_fuzzer.cc:vfseek(long, int, void*)
Line
Count
Source
19
444k
{
20
444k
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
444k
  sf_count_t new_offset ;
22
23
444k
  switch (whence)
24
444k
  {   case SEEK_SET :
25
72.0k
        new_offset = offset ;
26
72.0k
        break ;
27
28
372k
    case SEEK_CUR :
29
372k
        new_offset = vf->offset + offset ;
30
372k
        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
444k
  }
42
43
  /* Ensure you can't seek outside the data */
44
444k
  if (new_offset > vf->length)
45
2.92k
  {  /* Trying to seek past the end of the data */
46
2.92k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
2.92k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
2.92k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
2.92k
     new_offset = vf->length ;
50
2.92k
  }
51
441k
  else if (new_offset < 0)
52
277k
  {  /* Trying to seek before the start of the data */
53
277k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
277k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
277k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
277k
     new_offset = 0 ;
57
277k
  }
58
444k
  vf->offset = new_offset ;
59
60
444k
  return vf->offset ;
61
444k
}
sndfile_fuzzer.cc:vfseek(long, int, void*)
Line
Count
Source
19
372k
{
20
372k
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
372k
  sf_count_t new_offset ;
22
23
372k
  switch (whence)
24
372k
  {   case SEEK_SET :
25
32.8k
        new_offset = offset ;
26
32.8k
        break ;
27
28
339k
    case SEEK_CUR :
29
339k
        new_offset = vf->offset + offset ;
30
339k
        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
372k
  }
42
43
  /* Ensure you can't seek outside the data */
44
372k
  if (new_offset > vf->length)
45
2.01k
  {  /* Trying to seek past the end of the data */
46
2.01k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
2.01k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
2.01k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
2.01k
     new_offset = vf->length ;
50
2.01k
  }
51
370k
  else if (new_offset < 0)
52
197k
  {  /* Trying to seek before the start of the data */
53
197k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
197k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
197k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
197k
     new_offset = 0 ;
57
197k
  }
58
372k
  vf->offset = new_offset ;
59
60
372k
  return vf->offset ;
61
372k
}
62
63
static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data)
64
27.0M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
27.0M
   if (vf->offset + count > vf->length)
67
16.1M
     count = vf->length - vf->offset ;
68
69
27.0M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
27.0M
   vf->offset += count ;
71
72
27.0M
   return count ;
73
27.0M
}
sndfile_alt_fuzzer.cc:vfread(void*, long, void*)
Line
Count
Source
64
15.7M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
15.7M
   if (vf->offset + count > vf->length)
67
9.28M
     count = vf->length - vf->offset ;
68
69
15.7M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
15.7M
   vf->offset += count ;
71
72
15.7M
   return count ;
73
15.7M
}
sndfile_fuzzer.cc:vfread(void*, long, void*)
Line
Count
Source
64
11.3M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
11.3M
   if (vf->offset + count > vf->length)
67
6.89M
     count = vf->length - vf->offset ;
68
69
11.3M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
11.3M
   vf->offset += count ;
71
72
11.3M
   return count ;
73
11.3M
}
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
4.80M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
4.80M
  return vf->offset ;
89
4.80M
}
sndfile_alt_fuzzer.cc:vftell(void*)
Line
Count
Source
86
2.69M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
2.69M
  return vf->offset ;
89
2.69M
}
sndfile_fuzzer.cc:vftell(void*)
Line
Count
Source
86
2.11M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
2.11M
  return vf->offset ;
89
2.11M
}
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
29.1k
{
97
   // Initialize the virtual IO structure.
98
29.1k
   vio->get_filelen = vfget_filelen ;
99
29.1k
   vio->seek = vfseek ;
100
29.1k
   vio->read = vfread ;
101
29.1k
   vio->write = vfwrite ;
102
29.1k
   vio->tell = vftell ;
103
104
   // Initialize the VIO user data.
105
29.1k
   vio_data->data = data ;
106
29.1k
   vio_data->length = size ;
107
29.1k
   vio_data->offset = 0 ;
108
109
29.1k
   memset(sndfile_info, 0, sizeof(SF_INFO)) ;
110
111
   // Try and open the virtual file.
112
29.1k
   *sndfile = sf_open_virtual(vio, SFM_READ, sndfile_info, vio_data) ;
113
114
29.1k
   if (sndfile_info->channels == 0)
115
22.2k
     return -1 ;
116
117
6.89k
   if (sndfile_info->channels > 1024 * 1024)
118
0
     return -1 ;
119
120
6.89k
   return 0;
121
6.89k
}
122
123
#endif