Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpff/ossfuzz/item_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libpff item type
3
 *
4
 * Copyright (C) 2008-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_libpff.h"
31
32
#if !defined( LIBPFF_HAVE_BFIO )
33
34
/* Opens a file using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBPFF_EXTERN \
38
int libpff_file_open_file_io_handle(
39
      libpff_file_t *file,
40
      libbfio_handle_t *file_io_handle,
41
      int access_flags,
42
      libpff_error_t **error );
43
44
#endif /* !defined( LIBPFF_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
      const uint8_t *data,
48
      size_t size )
49
3.28k
{
50
3.28k
  libbfio_handle_t *file_io_handle = NULL;
51
3.28k
  libpff_file_t *file              = NULL;
52
3.28k
  libpff_item_t *root_folder       = NULL;
53
3.28k
  libpff_item_t *sub_item          = NULL;
54
3.28k
  uint8_t item_type               = 0;
55
3.28k
  uint32_t identifier             = 0;
56
3.28k
  uint32_t number_of_entries      = 0;
57
3.28k
  int number_of_record_sets       = 0;
58
3.28k
  int number_of_sub_items         = 0;
59
60
3.28k
  if( libbfio_memory_range_initialize(
61
3.28k
       &file_io_handle,
62
3.28k
       NULL ) != 1 )
63
0
  {
64
0
    return( 0 );
65
0
  }
66
3.28k
  if( libbfio_memory_range_set(
67
3.28k
       file_io_handle,
68
3.28k
       (uint8_t *) data,
69
3.28k
       size,
70
3.28k
       NULL ) != 1 )
71
0
  {
72
0
    goto on_error_libbfio;
73
0
  }
74
3.28k
  if( libpff_file_initialize(
75
3.28k
       &file,
76
3.28k
       NULL ) != 1 )
77
0
  {
78
0
    goto on_error_libbfio;
79
0
  }
80
3.28k
  if( libpff_file_open_file_io_handle(
81
3.28k
       file,
82
3.28k
       file_io_handle,
83
3.28k
       LIBPFF_OPEN_READ,
84
3.28k
       NULL ) != 1 )
85
2.73k
  {
86
2.73k
    goto on_error_libpff_file;
87
2.73k
  }
88
549
  if( libpff_file_get_root_folder(
89
549
       file,
90
549
       &root_folder,
91
549
       NULL ) == 1 )
92
356
  {
93
356
    if( libpff_item_get_identifier(
94
356
         root_folder,
95
356
         &identifier,
96
356
         NULL ) != 1 )
97
0
    {
98
0
      goto on_error_libpff_root_folder;
99
0
    }
100
356
    if( libpff_item_get_type(
101
356
         root_folder,
102
356
         &item_type,
103
356
         NULL ) != 1 )
104
241
    {
105
241
      goto on_error_libpff_root_folder;
106
241
    }
107
115
    if( libpff_item_get_number_of_sub_items(
108
115
         root_folder,
109
115
         &number_of_sub_items,
110
115
         NULL ) != 1 )
111
0
    {
112
0
      goto on_error_libpff_root_folder;
113
0
    }
114
115
    if( number_of_sub_items > 0 )
115
15
    {
116
15
      if( libpff_item_get_sub_item(
117
15
           root_folder,
118
15
           0,
119
15
           &sub_item,
120
15
           NULL ) != 1 )
121
0
      {
122
0
        goto on_error_libpff_root_folder;
123
0
      }
124
15
      if( libpff_item_get_identifier(
125
15
           sub_item,
126
15
           &identifier,
127
15
           NULL ) != 1 )
128
0
      {
129
0
        goto on_error_libpff_sub_item;
130
0
      }
131
15
      if( libpff_item_get_type(
132
15
           sub_item,
133
15
           &item_type,
134
15
           NULL ) != 1 )
135
10
      {
136
10
        goto on_error_libpff_sub_item;
137
10
      }
138
5
      if( libpff_item_get_number_of_entries(
139
5
           sub_item,
140
5
           &number_of_entries,
141
5
           NULL ) != 1 )
142
0
      {
143
0
        goto on_error_libpff_sub_item;
144
0
      }
145
5
      if( libpff_item_get_number_of_record_sets(
146
5
           sub_item,
147
5
           &number_of_record_sets,
148
5
           NULL ) != 1 )
149
0
      {
150
0
        goto on_error_libpff_sub_item;
151
0
      }
152
15
 on_error_libpff_sub_item:
153
15
      libpff_item_free(
154
15
       &sub_item,
155
15
       NULL );
156
15
    }
157
356
 on_error_libpff_root_folder:
158
356
    libpff_item_free(
159
356
     &root_folder,
160
356
     NULL );
161
356
  }
162
549
  libpff_file_close(
163
549
   file,
164
549
   NULL );
165
166
3.28k
on_error_libpff_file:
167
3.28k
  libpff_file_free(
168
3.28k
   &file,
169
3.28k
   NULL );
170
171
3.28k
on_error_libbfio:
172
3.28k
  libbfio_handle_free(
173
3.28k
   &file_io_handle,
174
3.28k
   NULL );
175
176
3.28k
  return( 0 );
177
3.28k
}
178
179
} /* extern "C" */