Coverage Report

Created: 2025-10-10 07:06

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
46.6k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
46.6k
   return vf->length ;
16
46.6k
}
sndfile_alt_fuzzer.cc:vfget_filelen(void*)
Line
Count
Source
14
24.4k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
24.4k
   return vf->length ;
16
24.4k
}
sndfile_fuzzer.cc:vfget_filelen(void*)
Line
Count
Source
14
22.2k
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
15
22.2k
   return vf->length ;
16
22.2k
}
17
18
static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data)
19
1.04M
{
20
1.04M
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
1.04M
  sf_count_t new_offset ;
22
23
1.04M
  switch (whence)
24
1.04M
  {   case SEEK_SET :
25
87.5k
        new_offset = offset ;
26
87.5k
        break ;
27
28
955k
    case SEEK_CUR :
29
955k
        new_offset = vf->offset + offset ;
30
955k
        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
1.04M
  }
42
43
  /* Ensure you can't seek outside the data */
44
1.04M
  if (new_offset > vf->length)
45
5.85k
  {  /* Trying to seek past the end of the data */
46
5.85k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
5.85k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
5.85k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
5.85k
     new_offset = vf->length ;
50
5.85k
  }
51
1.03M
  else if (new_offset < 0)
52
588k
  {  /* Trying to seek before the start of the data */
53
588k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
588k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
588k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
588k
     new_offset = 0 ;
57
588k
  }
58
1.04M
  vf->offset = new_offset ;
59
60
1.04M
  return vf->offset ;
61
1.04M
}
sndfile_alt_fuzzer.cc:vfseek(long, int, void*)
Line
Count
Source
19
615k
{
20
615k
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
615k
  sf_count_t new_offset ;
22
23
615k
  switch (whence)
24
615k
  {   case SEEK_SET :
25
51.4k
        new_offset = offset ;
26
51.4k
        break ;
27
28
564k
    case SEEK_CUR :
29
564k
        new_offset = vf->offset + offset ;
30
564k
        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
615k
  }
42
43
  /* Ensure you can't seek outside the data */
44
615k
  if (new_offset > vf->length)
45
2.85k
  {  /* Trying to seek past the end of the data */
46
2.85k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
2.85k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
2.85k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
2.85k
     new_offset = vf->length ;
50
2.85k
  }
51
612k
  else if (new_offset < 0)
52
338k
  {  /* Trying to seek before the start of the data */
53
338k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
338k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
338k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
338k
     new_offset = 0 ;
57
338k
  }
58
615k
  vf->offset = new_offset ;
59
60
615k
  return vf->offset ;
61
615k
}
sndfile_fuzzer.cc:vfseek(long, int, void*)
Line
Count
Source
19
427k
{
20
427k
  VIO_DATA *vf = (VIO_DATA *)user_data ;
21
427k
  sf_count_t new_offset ;
22
23
427k
  switch (whence)
24
427k
  {   case SEEK_SET :
25
36.0k
        new_offset = offset ;
26
36.0k
        break ;
27
28
391k
    case SEEK_CUR :
29
391k
        new_offset = vf->offset + offset ;
30
391k
        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
427k
  }
42
43
  /* Ensure you can't seek outside the data */
44
427k
  if (new_offset > vf->length)
45
3.00k
  {  /* Trying to seek past the end of the data */
46
3.00k
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
47
3.00k
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
48
3.00k
            new_offset, vf->length, whence, vf->offset, offset) ;
49
3.00k
     new_offset = vf->length ;
50
3.00k
  }
51
424k
  else if (new_offset < 0)
52
250k
  {  /* Trying to seek before the start of the data */
53
250k
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
54
250k
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
55
250k
            new_offset, whence, vf->offset, vf->length, offset) ;
56
250k
     new_offset = 0 ;
57
250k
  }
58
427k
  vf->offset = new_offset ;
59
60
427k
  return vf->offset ;
61
427k
}
62
63
static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data)
64
31.3M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
31.3M
   if (vf->offset + count > vf->length)
67
18.0M
     count = vf->length - vf->offset ;
68
69
31.3M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
31.3M
   vf->offset += count ;
71
72
31.3M
   return count ;
73
31.3M
}
sndfile_alt_fuzzer.cc:vfread(void*, long, void*)
Line
Count
Source
64
16.5M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
16.5M
   if (vf->offset + count > vf->length)
67
9.33M
     count = vf->length - vf->offset ;
68
69
16.5M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
16.5M
   vf->offset += count ;
71
72
16.5M
   return count ;
73
16.5M
}
sndfile_fuzzer.cc:vfread(void*, long, void*)
Line
Count
Source
64
14.8M
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
65
66
14.8M
   if (vf->offset + count > vf->length)
67
8.75M
     count = vf->length - vf->offset ;
68
69
14.8M
   memcpy(ptr, vf->data + vf->offset, count) ;
70
14.8M
   vf->offset += count ;
71
72
14.8M
   return count ;
73
14.8M
}
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
7.44M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
7.44M
  return vf->offset ;
89
7.44M
}
sndfile_alt_fuzzer.cc:vftell(void*)
Line
Count
Source
86
4.94M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
4.94M
  return vf->offset ;
89
4.94M
}
sndfile_fuzzer.cc:vftell(void*)
Line
Count
Source
86
2.49M
{ VIO_DATA *vf = (VIO_DATA *)user_data ;
87
88
2.49M
  return vf->offset ;
89
2.49M
}
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
42.3k
{
97
   // Initialize the virtual IO structure.
98
42.3k
   vio->get_filelen = vfget_filelen ;
99
42.3k
   vio->seek = vfseek ;
100
42.3k
   vio->read = vfread ;
101
42.3k
   vio->write = vfwrite ;
102
42.3k
   vio->tell = vftell ;
103
104
   // Initialize the VIO user data.
105
42.3k
   vio_data->data = data ;
106
42.3k
   vio_data->length = size ;
107
42.3k
   vio_data->offset = 0 ;
108
109
42.3k
   memset(sndfile_info, 0, sizeof(SF_INFO)) ;
110
111
   // Try and open the virtual file.
112
42.3k
   *sndfile = sf_open_virtual(vio, SFM_READ, sndfile_info, vio_data) ;
113
114
42.3k
   if (sndfile_info->channels == 0)
115
33.7k
     return -1 ;
116
117
8.58k
   if (sndfile_info->channels > 1024 * 1024)
118
0
     return -1 ;
119
120
8.58k
   return 0;
121
8.58k
}
122
123
#endif