Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libodraw/ossfuzz/handle_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libodraw handle type
3
 *
4
 * Copyright (C) 2010-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <stddef.h>
23
#include <stdint.h>
24
25
/* Note that some of the OSS-Fuzz engines use C++
26
 */
27
extern "C" {
28
29
#include "ossfuzz_libbfio.h"
30
#include "ossfuzz_libodraw.h"
31
32
#if !defined( LIBODRAW_HAVE_BFIO )
33
34
/* Opens a handle using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBODRAW_EXTERN \
38
int libodraw_handle_open_file_io_handle(
39
      libodraw_handle_t *handle,
40
      libbfio_handle_t *file_io_handle,
41
      int access_flags,
42
      libodraw_error_t **error );
43
44
/* Opens the data files using a Basic File IO (bfio) pool
45
 * This function assumes the data files are in same order as defined by the table of contents (TOC) file
46
 * Returns 1 if successful or -1 on error
47
 */
48
LIBODRAW_EXTERN \
49
int libodraw_handle_open_data_files_file_io_pool(
50
      libodraw_handle_t *handle,
51
      libbfio_pool_t *file_io_pool,
52
      libodraw_error_t **error );
53
54
#endif /* !defined( LIBODRAW_HAVE_BFIO ) */
55
56
int LLVMFuzzerTestOneInput(
57
      const uint8_t *data,
58
      size_t size )
59
1.93k
{
60
1.93k
  uint8_t buffer[ 512 ];
61
62
1.93k
  libodraw_data_file_t *data_file          = NULL;
63
1.93k
  libbfio_handle_t *file_io_handle         = NULL;
64
1.93k
  libbfio_pool_t *file_io_pool             = NULL;
65
1.93k
  libodraw_handle_t *handle                = NULL;
66
1.93k
  off64_t media_offset                     = 0;
67
1.93k
  size64_t media_size                      = 0;
68
1.93k
  uint64_t lead_out_start_sector          = 0;
69
1.93k
  uint64_t lead_out_number_of_sectors     = 0;
70
1.93k
  uint64_t number_of_sectors              = 0;
71
1.93k
  uint64_t session_start_sector           = 0;
72
1.93k
  uint64_t session_number_of_sectors      = 0;
73
1.93k
  uint32_t bytes_per_sector               = 0;
74
1.93k
  uint8_t track_type                      = 0;
75
1.93k
  uint64_t track_start_sector             = 0;
76
1.93k
  uint64_t track_number_of_sectors        = 0;
77
1.93k
  uint64_t track_data_file_start_sector   = 0;
78
1.93k
  int data_file_index                    = 0;
79
1.93k
  int entry_index                        = 0;
80
1.93k
  int number_of_data_files               = 0;
81
1.93k
  int number_of_lead_outs                = 0;
82
1.93k
  int number_of_sessions                 = 0;
83
1.93k
  int number_of_tracks                   = 0;
84
1.93k
  int track_index                        = 0;
85
1.93k
  int track_data_file_index              = 0;
86
1.93k
  int read_iterator                      = 0;
87
88
1.93k
  if( libbfio_memory_range_initialize(
89
1.93k
       &file_io_handle,
90
1.93k
       NULL ) != 1 )
91
0
  {
92
0
    return( 0 );
93
0
  }
94
1.93k
  if( libbfio_memory_range_set(
95
1.93k
       file_io_handle,
96
1.93k
       (uint8_t *) data,
97
1.93k
       size,
98
1.93k
       NULL ) != 1 )
99
0
  {
100
0
    goto on_error_libbfio;
101
0
  }
102
1.93k
  if( libbfio_pool_initialize(
103
1.93k
       &file_io_pool,
104
1.93k
       0,
105
1.93k
       0,
106
1.93k
       NULL ) != 1 )
107
0
  {
108
0
    goto on_error_libbfio;
109
0
  }
110
1.93k
  if( libodraw_handle_initialize(
111
1.93k
       &handle,
112
1.93k
       NULL ) != 1 )
113
0
  {
114
0
    goto on_error_libbfio;
115
0
  }
116
1.93k
  if( libodraw_handle_open_file_io_handle(
117
1.93k
       handle,
118
1.93k
       file_io_handle,
119
1.93k
       LIBODRAW_OPEN_READ,
120
1.93k
       NULL ) != 1 )
121
812
  {
122
812
    goto on_error_libodraw;
123
812
  }
124
1.12k
  if( libbfio_pool_append_handle(
125
1.12k
       file_io_pool,
126
1.12k
       &entry_index,
127
1.12k
       file_io_handle,
128
1.12k
       LIBBFIO_OPEN_READ,
129
1.12k
       NULL ) != 1 )
130
0
  {
131
0
    goto on_error_libodraw;
132
0
  }
133
  /* The file IO pool takes over management of the file IO handle
134
   */
135
1.12k
  file_io_handle = NULL;
136
137
1.12k
  if( libodraw_handle_open_data_files_file_io_pool(
138
1.12k
       handle,
139
1.12k
       file_io_pool,
140
1.12k
       NULL ) != 1 )
141
663
  {
142
663
    goto on_error_libodraw;
143
663
  }
144
458
  if( libodraw_handle_get_bytes_per_sector(
145
458
       handle,
146
458
       &bytes_per_sector,
147
458
       NULL ) != 1 )
148
0
  {
149
0
    goto on_error_libodraw;
150
0
  }
151
458
  if( libodraw_handle_get_number_of_sectors(
152
458
       handle,
153
458
       &number_of_sectors,
154
458
       NULL ) != 1 )
155
0
  {
156
0
    goto on_error_libodraw;
157
0
  }
158
458
  if( libodraw_handle_get_media_size(
159
458
       handle,
160
458
       &media_size,
161
458
       NULL ) != 1 )
162
0
  {
163
0
    goto on_error_libodraw;
164
0
  }
165
458
  if( libodraw_handle_get_number_of_data_files(
166
458
       handle,
167
458
       &number_of_data_files,
168
458
       NULL ) == 1 )
169
458
  {
170
458
    for( data_file_index = 0;
171
5.56k
         data_file_index < number_of_data_files;
172
5.10k
         data_file_index++ )
173
5.34k
    {
174
5.34k
      if( data_file_index >= 16 )
175
240
      {
176
240
        break;
177
240
      }
178
5.10k
      if( libodraw_handle_get_data_file(
179
5.10k
           handle,
180
5.10k
           data_file_index,
181
5.10k
           &data_file,
182
5.10k
           NULL ) != 1 )
183
0
      {
184
0
        goto on_error_libodraw;
185
0
      }
186
5.10k
      if( libodraw_data_file_free(
187
5.10k
           &data_file,
188
5.10k
           NULL ) != 1 )
189
0
      {
190
0
        goto on_error_libodraw;
191
0
      }
192
5.10k
    }
193
458
  }
194
458
  if( libodraw_handle_get_number_of_sessions(
195
458
       handle,
196
458
       &number_of_sessions,
197
458
       NULL ) == 1 )
198
458
  {
199
458
    if( number_of_sessions > 0 )
200
0
    {
201
0
      if( libodraw_handle_get_session(
202
0
           handle,
203
0
           0,
204
0
           &session_start_sector,
205
0
           &session_number_of_sectors,
206
0
           NULL ) != 1 )
207
0
      {
208
0
        goto on_error_libodraw;
209
0
      }
210
0
    }
211
458
  }
212
458
  if( libodraw_handle_get_number_of_lead_outs(
213
458
       handle,
214
458
       &number_of_lead_outs,
215
458
       NULL ) == 1 )
216
458
  {
217
458
    if( number_of_lead_outs > 0 )
218
0
    {
219
0
      if( libodraw_handle_get_lead_out(
220
0
           handle,
221
0
           0,
222
0
           &lead_out_start_sector,
223
0
           &lead_out_number_of_sectors,
224
0
           NULL ) != 1 )
225
0
      {
226
0
        goto on_error_libodraw;
227
0
      }
228
0
    }
229
458
  }
230
458
  if( libodraw_handle_get_number_of_tracks(
231
458
       handle,
232
458
       &number_of_tracks,
233
458
       NULL ) == 1 )
234
458
  {
235
458
    for( track_index = 0;
236
4.93k
         track_index < number_of_tracks;
237
4.47k
         track_index++ )
238
4.64k
    {
239
4.64k
      if( track_index >= 16 )
240
164
      {
241
164
        break;
242
164
      }
243
4.47k
      if( libodraw_handle_get_track(
244
4.47k
           handle,
245
4.47k
           track_index,
246
4.47k
           &track_start_sector,
247
4.47k
           &track_number_of_sectors,
248
4.47k
           &track_type,
249
4.47k
           &track_data_file_index,
250
4.47k
           &track_data_file_start_sector,
251
4.47k
           NULL ) != 1 )
252
0
      {
253
0
        goto on_error_libodraw;
254
0
      }
255
4.47k
    }
256
458
  }
257
458
  for( read_iterator = 0;
258
11.2k
       read_iterator < 128;
259
10.7k
       read_iterator++ )
260
11.2k
  {
261
11.2k
    if( media_offset >= media_size )
262
241
    {
263
241
      break;
264
241
    }
265
10.9k
    if( libodraw_handle_read_buffer_at_offset(
266
10.9k
         handle,
267
10.9k
         buffer,
268
10.9k
         497,
269
10.9k
         media_offset,
270
10.9k
         NULL ) == -1 )
271
178
    {
272
178
      goto on_error_libodraw;
273
178
    }
274
10.7k
    media_offset += 497;
275
10.7k
  }
276
280
  libodraw_handle_close(
277
280
   handle,
278
280
   NULL );
279
280
1.93k
on_error_libodraw:
281
1.93k
  libodraw_handle_free(
282
1.93k
   &handle,
283
1.93k
   NULL );
284
285
1.93k
on_error_libbfio:
286
  /* Note that on error the handle still has a reference to file_io_pool
287
   * that will be closed. Therefore the file IO pool and handle need to
288
   * be freed after closing or freeing the handle.
289
   */
290
1.93k
  if( file_io_pool != NULL )
291
1.93k
  {
292
1.93k
    libbfio_pool_free(
293
1.93k
     &file_io_pool,
294
1.93k
     NULL );
295
1.93k
  }
296
1.93k
  if( file_io_handle != NULL )
297
812
  {
298
812
    libbfio_handle_free(
299
812
     &file_io_handle,
300
     NULL );
301
812
  }
302
1.93k
  return( 0 );
303
1.93k
}
304
305
} /* extern "C" */