Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfusn/ossfuzz/record_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libfusn record type
3
 *
4
 * Copyright (C) 2011-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
#include <stdio.h>
25
#include <stdlib.h>
26
27
/* Note that some of the OSS-Fuzz engines use C++
28
 */
29
extern "C" {
30
31
#include "ossfuzz_libfusn.h"
32
33
int LLVMFuzzerTestOneInput(
34
     const uint8_t *data,
35
     size_t size )
36
485
{
37
485
  const char *version           = NULL;
38
485
  libfusn_error_t *error        = NULL;
39
485
  libfusn_record_t *record      = NULL;
40
485
  uint16_t *utf16_name          = NULL;
41
485
  uint8_t *utf8_name            = NULL;
42
485
  uint32_t flags                = 0;
43
485
  uint32_t record_size          = 0;
44
485
  uint64_t value_64bit          = 0;
45
485
  int result                    = 0;
46
485
  size_t utf16_name_size        = 0;
47
485
  size_t utf8_name_size         = 0;
48
485
  char error_message[ 4000 ];
49
50
485
  version  = libfusn_get_version();
51
52
485
  if( version == NULL )
53
0
  {
54
0
    return( 0 );
55
0
  }
56
485
  if( libfusn_record_initialize(
57
485
       &record,
58
485
       &error ) != 1 )
59
0
  {
60
0
    if( error != NULL )
61
0
    {
62
0
      libfusn_error_free(
63
0
       &error );
64
0
    }
65
0
    return( 0 );
66
0
  }
67
485
  if( libfusn_record_copy_from_byte_stream(
68
485
       record,
69
485
       data,
70
485
       size,
71
485
       &error ) != 1 )
72
160
  {
73
160
    if( error != NULL )
74
160
    {
75
      /* Exercise the library error functions
76
       */
77
160
      libfusn_error_backtrace_fprint(
78
160
       error,
79
160
       stderr );
80
81
160
      libfusn_error_backtrace_sprint(
82
160
       error,
83
160
       error_message,
84
160
       sizeof( error_message ) );
85
86
160
      libfusn_error_fprint(
87
160
       error,
88
160
       stderr );
89
90
160
      libfusn_error_sprint(
91
160
       error,
92
160
       error_message,
93
160
       sizeof( error_message ) );
94
95
160
      libfusn_error_free(
96
160
       &error );
97
160
    }
98
160
  }
99
100
  /* Exercise the "record was already copied" branch
101
   */
102
485
  libfusn_record_copy_from_byte_stream(
103
485
   record,
104
485
   data,
105
485
   size,
106
485
   NULL );
107
108
  /* Exercise the record getter functions
109
   * also for records that failed to parse
110
   */
111
485
  libfusn_record_get_size(
112
485
   record,
113
485
   &record_size,
114
485
   NULL );
115
116
485
  libfusn_record_get_update_time(
117
485
   record,
118
485
   &value_64bit,
119
485
   NULL );
120
121
485
  libfusn_record_get_file_reference(
122
485
   record,
123
485
   &value_64bit,
124
485
   NULL );
125
126
485
  libfusn_record_get_parent_file_reference(
127
485
   record,
128
485
   &value_64bit,
129
485
   NULL );
130
131
485
  libfusn_record_get_update_sequence_number(
132
485
   record,
133
485
   &value_64bit,
134
485
   NULL );
135
136
485
  libfusn_record_get_update_reason_flags(
137
485
   record,
138
485
   &flags,
139
485
   NULL );
140
141
485
  libfusn_record_get_update_source_flags(
142
485
   record,
143
485
   &flags,
144
485
   NULL );
145
146
485
  libfusn_record_get_file_attribute_flags(
147
485
   record,
148
485
   &flags,
149
485
   NULL );
150
151
  /* Exercise the UTF-8 and UTF-16 name getter functions
152
   * including those for records without a name
153
   */
154
485
  libfusn_record_get_utf8_name_size(
155
485
   record,
156
485
   NULL,
157
485
   &error );
158
159
485
  libfusn_error_free(
160
485
   &error );
161
162
485
  libfusn_record_get_utf8_name(
163
485
   record,
164
485
   NULL,
165
485
   1,
166
485
   &error );
167
168
485
  libfusn_error_free(
169
485
   &error );
170
171
485
  libfusn_record_get_utf16_name_size(
172
485
   record,
173
485
   NULL,
174
485
   &error );
175
176
485
  libfusn_error_free(
177
485
   &error );
178
179
485
  libfusn_record_get_utf16_name(
180
485
   record,
181
485
   NULL,
182
485
   1,
183
485
   &error );
184
185
485
  libfusn_error_free(
186
485
   &error );
187
188
485
  result = libfusn_record_get_utf8_name_size(
189
485
           record,
190
485
           &utf8_name_size,
191
485
           &error );
192
193
485
  libfusn_error_free(
194
485
   &error );
195
196
485
  if( result == 1 )
197
418
  {
198
418
    if( utf8_name_size > 0 )
199
241
    {
200
241
      utf8_name = (uint8_t *) malloc(
201
241
                                       utf8_name_size );
202
203
241
      if( utf8_name != NULL )
204
241
      {
205
        /* Exercise the "UTF-8 string too small" branch
206
         */
207
241
        libfusn_record_get_utf8_name(
208
241
         record,
209
241
         utf8_name,
210
241
         1,
211
241
         &error );
212
213
241
        libfusn_error_free(
214
241
         &error );
215
216
        /* Exercise the UTF-16 stream to UTF-8 string conversion
217
         */
218
241
        libfusn_record_get_utf8_name(
219
241
         record,
220
241
         utf8_name,
221
241
         utf8_name_size,
222
241
         NULL );
223
224
241
        free(
225
241
         utf8_name );
226
241
      }
227
241
    }
228
418
  }
229
485
  result = libfusn_record_get_utf16_name_size(
230
485
           record,
231
485
           &utf16_name_size,
232
485
           &error );
233
234
485
  libfusn_error_free(
235
485
   &error );
236
237
485
  if( result == 1 )
238
418
  {
239
418
    if( utf16_name_size > 0 )
240
241
    {
241
241
      utf16_name = (uint16_t *) malloc(
242
241
                                         utf16_name_size * sizeof( uint16_t ) );
243
244
241
      if( utf16_name != NULL )
245
241
      {
246
        /* Exercise the "UTF-16 string too small" branch
247
         */
248
241
        libfusn_record_get_utf16_name(
249
241
         record,
250
241
         utf16_name,
251
241
         1,
252
241
         &error );
253
254
241
        libfusn_error_free(
255
241
         &error );
256
257
        /* Exercise the UTF-16 stream to UTF-16 string conversion
258
         */
259
241
        libfusn_record_get_utf16_name(
260
241
         record,
261
241
         utf16_name,
262
241
         utf16_name_size,
263
241
         NULL );
264
265
241
        free(
266
241
         utf16_name );
267
241
      }
268
241
    }
269
418
  }
270
  /* Exercise the "invalid record" error branches of the getter functions
271
   */
272
485
  libfusn_record_get_size(
273
485
   NULL,
274
485
   NULL,
275
485
   NULL );
276
277
485
  libfusn_record_get_update_time(
278
485
   NULL,
279
485
   NULL,
280
485
   NULL );
281
282
485
  libfusn_record_get_file_reference(
283
485
   NULL,
284
485
   NULL,
285
485
   NULL );
286
287
485
  libfusn_record_get_parent_file_reference(
288
485
   NULL,
289
485
   NULL,
290
485
   NULL );
291
292
485
  libfusn_record_get_update_sequence_number(
293
485
   NULL,
294
485
   NULL,
295
485
   NULL );
296
297
485
  libfusn_record_get_update_reason_flags(
298
485
   NULL,
299
485
   NULL,
300
485
   NULL );
301
302
485
  libfusn_record_get_update_source_flags(
303
485
   NULL,
304
485
   NULL,
305
485
   NULL );
306
307
485
  libfusn_record_get_file_attribute_flags(
308
485
   NULL,
309
485
   NULL,
310
485
   NULL );
311
312
485
  libfusn_record_get_utf8_name_size(
313
485
   NULL,
314
485
   NULL,
315
485
   NULL );
316
317
485
  libfusn_record_get_utf8_name(
318
485
   NULL,
319
485
   NULL,
320
485
   1,
321
485
   NULL );
322
323
485
  libfusn_record_get_utf16_name_size(
324
485
   NULL,
325
485
   NULL,
326
485
   NULL );
327
328
485
  libfusn_record_get_utf16_name(
329
485
   NULL,
330
485
   NULL,
331
485
   1,
332
485
   NULL );
333
334
  /* Exercise the "invalid record" and "record already set" error branches
335
   * of the initialize function and the "invalid record" error branch of
336
   * the free function
337
   */
338
485
  libfusn_record_initialize(
339
485
   NULL,
340
485
   NULL );
341
342
485
  libfusn_record_initialize(
343
485
   &record,
344
485
   NULL );
345
346
485
  libfusn_record_free(
347
485
   NULL,
348
485
   NULL );
349
350
485
  if( libfusn_record_free(
351
485
       &record,
352
485
       &error ) != 1 )
353
0
  {
354
0
    if( error != NULL )
355
0
    {
356
0
      libfusn_error_free(
357
0
       &error );
358
0
    }
359
0
  }
360
485
  return( 0 );
361
485
}
362
363
} /* extern "C" */