Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmodi/ossfuzz/handle_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libmodi file type
3
 *
4
 * Copyright (C) 2012-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_libmodi.h"
31
32
#if !defined( LIBMODI_HAVE_BFIO )
33
34
/* Determines if a file contains a Mac OS disk image file signature
35
 * using a Basic File IO (bfio) handle
36
 * Returns 1 if true, 0 if not or -1 on error
37
 */
38
LIBMODI_EXTERN \
39
int libmodi_check_file_signature_file_io_handle(
40
     libbfio_handle_t *file_io_handle,
41
     libmodi_error_t **error );
42
43
/* Opens a handle using a Basic File IO (bfio) handle
44
 * Returns 1 if successful or -1 on error
45
 */
46
LIBMODI_EXTERN \
47
int libmodi_handle_open_file_io_handle(
48
     libmodi_handle_t *handle,
49
     libbfio_handle_t *file_io_handle,
50
     int access_flags,
51
     libmodi_error_t **error );
52
53
#endif /* !defined( LIBMODI_HAVE_BFIO ) */
54
55
int LLVMFuzzerTestOneInput(
56
     const uint8_t *data,
57
     size_t size )
58
3.87k
{
59
3.87k
  uint8_t buffer[ 512 ];
60
61
3.87k
  libbfio_handle_t *file_io_handle = NULL;
62
3.87k
  libmodi_handle_t *handle         = NULL;
63
3.87k
  ssize_t read_count               = 0;
64
3.87k
  off64_t media_offset             = 0;
65
3.87k
  size64_t media_size              = 0;
66
3.87k
  int image_type                   = 0;
67
3.87k
  int read_iterator                = 0;
68
69
3.87k
  if( libbfio_memory_range_initialize(
70
3.87k
       &file_io_handle,
71
3.87k
       NULL ) != 1 )
72
0
  {
73
0
    return( 0 );
74
0
  }
75
3.87k
  if( libbfio_memory_range_set(
76
3.87k
       file_io_handle,
77
3.87k
       (uint8_t *) data,
78
3.87k
       size,
79
3.87k
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
3.87k
  if( libmodi_check_file_signature_file_io_handle(
84
3.87k
       file_io_handle,
85
3.87k
       NULL ) == -1 )
86
0
  {
87
0
    goto on_error_libbfio;
88
0
  }
89
3.87k
  if( libmodi_handle_initialize(
90
3.87k
       &handle,
91
3.87k
       NULL ) != 1 )
92
0
  {
93
0
    goto on_error_libbfio;
94
0
  }
95
3.87k
  if( libmodi_handle_open_file_io_handle(
96
3.87k
       handle,
97
3.87k
       file_io_handle,
98
3.87k
       LIBMODI_OPEN_READ,
99
3.87k
       NULL ) != 1 )
100
2.43k
  {
101
2.43k
    goto on_error_libmodi;
102
2.43k
  }
103
1.44k
  if( libmodi_handle_set_maximum_number_of_open_handles(
104
1.44k
       handle,
105
1.44k
       8,
106
1.44k
       NULL ) != 1 )
107
0
  {
108
0
    goto on_error_libmodi;
109
0
  }
110
1.44k
  if( libmodi_handle_get_image_type(
111
1.44k
       handle,
112
1.44k
       &image_type,
113
1.44k
       NULL ) != 1 )
114
0
  {
115
0
    goto on_error_libmodi;
116
0
  }
117
1.44k
  if( libmodi_handle_get_media_size(
118
1.44k
       handle,
119
1.44k
       &media_size,
120
1.44k
       NULL ) != 1 )
121
0
  {
122
0
    goto on_error_libmodi;
123
0
  }
124
1.44k
  for( read_iterator = 0;
125
50.2k
       read_iterator < 512;
126
48.8k
       read_iterator++ )
127
50.1k
  {
128
50.1k
    read_count = libmodi_handle_read_buffer(
129
50.1k
                      handle,
130
50.1k
                      buffer,
131
50.1k
                      497,
132
50.1k
                      NULL );
133
134
50.1k
    if( read_count == -1 )
135
1.10k
    {
136
1.10k
      goto on_error_libmodi;
137
1.10k
    }
138
49.0k
    if( read_count == 0 )
139
266
    {
140
266
      break;
141
266
    }
142
49.0k
  }
143
335
  if( media_size > 0 )
144
264
  {
145
264
    if( libmodi_handle_seek_offset(
146
264
         handle,
147
264
         (off64_t) ( media_size / 2 ),
148
264
         SEEK_SET,
149
264
         NULL ) == -1 )
150
0
    {
151
0
      goto on_error_libmodi;
152
0
    }
153
264
  }
154
335
  if( libmodi_handle_seek_offset(
155
335
       handle,
156
335
       512,
157
335
       SEEK_CUR,
158
335
       NULL ) == -1 )
159
0
  {
160
0
    goto on_error_libmodi;
161
0
  }
162
335
  if( libmodi_handle_seek_offset(
163
335
       handle,
164
335
       0,
165
335
       SEEK_END,
166
335
       NULL ) == -1 )
167
0
  {
168
0
    goto on_error_libmodi;
169
0
  }
170
335
  if( libmodi_handle_get_offset(
171
335
       handle,
172
335
       &media_offset,
173
335
       NULL ) != 1 )
174
0
  {
175
0
    goto on_error_libmodi;
176
0
  }
177
335
  read_count = libmodi_handle_read_buffer(
178
335
                    handle,
179
335
                    buffer,
180
335
                    497,
181
335
                    NULL );
182
183
335
  if( read_count == -1 )
184
0
  {
185
0
    goto on_error_libmodi;
186
0
  }
187
335
  if( libmodi_handle_seek_offset(
188
335
       handle,
189
335
       0,
190
335
       SEEK_SET,
191
335
       NULL ) == -1 )
192
0
  {
193
0
    goto on_error_libmodi;
194
0
  }
195
335
  media_offset = 0;
196
197
335
  for( read_iterator = 0;
198
96.4k
       read_iterator < 512;
199
96.1k
       read_iterator++ )
200
96.2k
  {
201
96.2k
    if( media_offset >= media_size )
202
162
    {
203
162
      break;
204
162
    }
205
96.1k
    if( libmodi_handle_read_buffer_at_offset(
206
96.1k
         handle,
207
96.1k
         buffer,
208
96.1k
         497,
209
96.1k
         media_offset,
210
96.1k
         NULL ) == -1 )
211
0
    {
212
0
      goto on_error_libmodi;
213
0
    }
214
96.1k
    media_offset += 497;
215
96.1k
  }
216
335
  if( libmodi_handle_signal_abort(
217
335
       handle,
218
335
       NULL ) != 1 )
219
0
  {
220
0
    goto on_error_libmodi;
221
0
  }
222
335
  libmodi_handle_close(
223
335
   handle,
224
335
   NULL );
225
226
3.87k
on_error_libmodi:
227
3.87k
  libmodi_handle_free(
228
3.87k
   &handle,
229
3.87k
   NULL );
230
231
3.87k
on_error_libbfio:
232
3.87k
  libbfio_handle_free(
233
3.87k
   &file_io_handle,
234
3.87k
   NULL );
235
236
3.87k
  return( 0 );
237
3.87k
}
238
239
} /* extern "C" */