Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libagdb/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libagdb file type
3
 *
4
 * Copyright (C) 2014-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
#include <stdlib.h>
26
27
/* Note that some of the OSS-Fuzz engines use C++
28
 */
29
extern "C" {
30
31
#include "ossfuzz_libagdb.h"
32
#include "ossfuzz_libbfio.h"
33
34
#if !defined( LIBAGDB_HAVE_BFIO )
35
36
/* Opens a file using a Basic File IO (bfio) handle
37
 * Returns 1 if successful or -1 on error
38
 */
39
LIBAGDB_EXTERN \
40
int libagdb_file_open_file_io_handle(
41
      libagdb_file_t *file,
42
      libbfio_handle_t *file_io_handle,
43
      int access_flags,
44
      libagdb_error_t **error );
45
46
#endif /* !defined( LIBAGDB_HAVE_BFIO ) */
47
48
int LLVMFuzzerTestOneInput(
49
      const uint8_t *data,
50
      size_t size )
51
2.73k
{
52
2.73k
  libbfio_handle_t *file_io_handle                 = NULL;
53
2.73k
  libagdb_file_t *file                             = NULL;
54
2.73k
  libagdb_volume_information_t *volume_information = NULL;
55
2.73k
  libagdb_file_information_t *file_information     = NULL;
56
2.73k
  libagdb_source_information_t *source_information = NULL;
57
2.73k
  size_t value_string_size                         = 0;
58
2.73k
  uint64_t value_64bit                             = 0;
59
2.73k
  uint32_t value_32bit                             = 0;
60
2.73k
  uint8_t *value_string                            = NULL;
61
2.73k
  int number_of_files                              = 0;
62
2.73k
  int number_of_sources                            = 0;
63
2.73k
  int number_of_volumes                            = 0;
64
2.73k
  int source_index                                 = 0;
65
2.73k
  int volume_index                                 = 0;
66
67
2.73k
  if( libbfio_memory_range_initialize(
68
2.73k
       &file_io_handle,
69
2.73k
       NULL ) != 1 )
70
0
  {
71
0
    return( 0 );
72
0
  }
73
2.73k
  if( libbfio_memory_range_set(
74
2.73k
       file_io_handle,
75
2.73k
       (uint8_t *) data,
76
2.73k
       size,
77
2.73k
       NULL ) != 1 )
78
0
  {
79
0
    goto on_error_libbfio;
80
0
  }
81
2.73k
  if( libagdb_file_initialize(
82
2.73k
       &file,
83
2.73k
       NULL ) != 1 )
84
0
  {
85
0
    goto on_error_libbfio;
86
0
  }
87
2.73k
  if( libagdb_file_open_file_io_handle(
88
2.73k
       file,
89
2.73k
       file_io_handle,
90
2.73k
       LIBAGDB_OPEN_READ,
91
2.73k
       NULL ) != 1 )
92
2.34k
  {
93
2.34k
    goto on_error_libagdb;
94
2.34k
  }
95
389
  if( libagdb_file_get_number_of_volumes(
96
389
       file,
97
389
       &number_of_volumes,
98
389
       NULL ) == 1 )
99
389
  {
100
389
    for( volume_index = 0;
101
947
         volume_index < number_of_volumes
102
564
     && volume_index < 8;
103
558
         volume_index++ )
104
558
    {
105
558
      if( libagdb_file_get_volume_information(
106
558
           file,
107
558
           volume_index,
108
558
           &volume_information,
109
558
           NULL ) != 1 )
110
0
      {
111
0
        continue;
112
0
      }
113
558
      libagdb_volume_information_get_creation_time(
114
558
       volume_information,
115
558
       &value_64bit,
116
558
       NULL );
117
118
558
      libagdb_volume_information_get_serial_number(
119
558
       volume_information,
120
558
       &value_32bit,
121
558
       NULL );
122
123
558
      if( ( libagdb_volume_information_get_utf8_device_path_size(
124
558
            volume_information,
125
558
            &value_string_size,
126
558
            NULL ) == 1 )
127
369
       && ( value_string_size > 0 )
128
369
       && ( value_string_size <= (size_t) ( 1 << 16 ) ) )
129
342
      {
130
342
        value_string = (uint8_t *) malloc(
131
342
                                    value_string_size );
132
133
342
        if( value_string != NULL )
134
342
        {
135
342
          libagdb_volume_information_get_utf8_device_path(
136
342
           volume_information,
137
342
           value_string,
138
342
           value_string_size,
139
342
           NULL );
140
342
          free( value_string );
141
142
342
          value_string = NULL;
143
342
        }
144
342
      }
145
558
      if( ( libagdb_volume_information_get_utf16_device_path_size(
146
558
            volume_information,
147
558
            &value_string_size,
148
558
            NULL ) == 1 )
149
369
       && ( value_string_size > 0 )
150
369
       && ( value_string_size <= ( (size_t) ( 1 << 16 ) / sizeof( uint16_t ) ) ) )
151
366
      {
152
366
        value_string = (uint8_t *) malloc(
153
366
                                    value_string_size * sizeof( uint16_t ) );
154
155
366
        if( value_string != NULL )
156
366
        {
157
366
          libagdb_volume_information_get_utf16_device_path(
158
366
           volume_information,
159
366
           (uint16_t *) value_string,
160
366
           value_string_size,
161
366
           NULL );
162
366
          free( value_string );
163
164
366
          value_string = NULL;
165
366
        }
166
366
      }
167
558
      if( ( libagdb_volume_information_get_number_of_files(
168
558
            volume_information,
169
558
            &number_of_files,
170
558
            NULL ) == 1 )
171
558
       && ( number_of_files > 0 ) )
172
84
      {
173
84
        if( libagdb_volume_information_get_file_information(
174
84
             volume_information,
175
84
             0,
176
84
             &file_information,
177
84
             NULL ) == 1 )
178
84
        {
179
84
          if( ( libagdb_file_information_get_utf8_path_size(
180
84
                file_information,
181
84
                &value_string_size,
182
84
                NULL ) == 1 )
183
48
           && ( value_string_size > 0 )
184
37
           && ( value_string_size <= (size_t) ( 1 << 16 ) ) )
185
37
          {
186
37
            value_string = (uint8_t *) malloc(
187
37
                                        value_string_size );
188
189
37
            if( value_string != NULL )
190
37
            {
191
37
              libagdb_file_information_get_utf8_path(
192
37
               file_information,
193
37
               value_string,
194
37
               value_string_size,
195
37
               NULL );
196
37
              free( value_string );
197
198
37
              value_string = NULL;
199
37
            }
200
37
          }
201
84
          if( ( libagdb_file_information_get_utf16_path_size(
202
84
                file_information,
203
84
                &value_string_size,
204
84
                NULL ) == 1 )
205
48
           && ( value_string_size > 0 )
206
37
           && ( value_string_size <= ( (size_t) ( 1 << 16 ) / sizeof( uint16_t ) ) ) )
207
37
          {
208
37
            value_string = (uint8_t *) malloc(
209
37
                                        value_string_size * sizeof( uint16_t ) );
210
211
37
            if( value_string != NULL )
212
37
            {
213
37
              libagdb_file_information_get_utf16_path(
214
37
               file_information,
215
37
               (uint16_t *) value_string,
216
37
               value_string_size,
217
37
               NULL );
218
37
              free( value_string );
219
220
37
              value_string = NULL;
221
37
            }
222
37
          }
223
84
          libagdb_file_information_free(
224
84
           &file_information,
225
84
           NULL );
226
84
        }
227
84
      }
228
558
      libagdb_volume_information_free(
229
558
       &volume_information,
230
558
       NULL );
231
558
    }
232
389
  }
233
389
  if( libagdb_file_get_number_of_sources(
234
389
       file,
235
389
       &number_of_sources,
236
389
       NULL ) == 1 )
237
389
  {
238
389
    for( source_index = 0;
239
505
         source_index < number_of_sources
240
122
     && source_index < 8;
241
389
         source_index++ )
242
116
    {
243
116
      if( libagdb_file_get_source_information(
244
116
           file,
245
116
           source_index,
246
116
           &source_information,
247
116
           NULL ) != 1 )
248
0
      {
249
0
        continue;
250
0
      }
251
116
      if( ( libagdb_source_information_get_utf8_executable_filename_size(
252
116
            source_information,
253
116
            &value_string_size,
254
116
            NULL ) == 1 )
255
116
       && ( value_string_size > 0 )
256
73
       && ( value_string_size <= (size_t) ( 1 << 16 ) ) )
257
73
      {
258
73
        value_string = (uint8_t *) malloc(
259
73
                                    value_string_size );
260
261
73
        if( value_string != NULL )
262
73
        {
263
73
          libagdb_source_information_get_utf8_executable_filename(
264
73
           source_information,
265
73
           value_string,
266
73
           value_string_size,
267
73
           NULL );
268
73
          free( value_string );
269
270
73
          value_string = NULL;
271
73
        }
272
73
      }
273
116
      if( ( libagdb_source_information_get_utf16_executable_filename_size(
274
116
            source_information,
275
116
            &value_string_size,
276
116
            NULL ) == 1 )
277
116
       && ( value_string_size > 0 )
278
73
       && ( value_string_size <= ( (size_t) ( 1 << 16 ) / sizeof( uint16_t ) ) ) )
279
73
      {
280
73
        value_string = (uint8_t *) malloc(
281
73
                                    value_string_size * sizeof( uint16_t ) );
282
283
73
        if( value_string != NULL )
284
73
        {
285
73
          libagdb_source_information_get_utf16_executable_filename(
286
73
           source_information,
287
73
           (uint16_t *) value_string,
288
73
           value_string_size,
289
73
           NULL );
290
73
          free( value_string );
291
292
73
          value_string = NULL;
293
73
        }
294
73
      }
295
116
      libagdb_source_information_free(
296
116
       &source_information,
297
116
       NULL );
298
116
    }
299
389
  }
300
389
  libagdb_file_close(
301
389
   file,
302
389
   NULL );
303
304
2.73k
on_error_libagdb:
305
2.73k
  libagdb_file_free(
306
2.73k
   &file,
307
2.73k
   NULL );
308
309
2.73k
on_error_libbfio:
310
2.73k
  libbfio_handle_free(
311
2.73k
   &file_io_handle,
312
2.73k
   NULL );
313
314
2.73k
  return( 0 );
315
2.73k
}
316
317
} /* extern "C" */