Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpff/ossfuzz/folder_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libpff folder 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
2.96k
{
50
2.96k
  char string[ 64 ];
51
52
2.96k
  libbfio_handle_t *file_io_handle    = NULL;
53
2.96k
  libpff_file_t *file                 = NULL;
54
2.96k
  libpff_item_t *root_folder          = NULL;
55
2.96k
  libpff_item_t *sub_folder           = NULL;
56
2.96k
  uint16_t unicode_string[ 32 ];
57
2.96k
  size_t string_size                 = 0;
58
2.96k
  int number_of_sub_folders          = 0;
59
2.96k
  int number_of_sub_messages         = 0;
60
2.96k
  int number_of_sub_associated_contents = 0;
61
62
2.96k
  if( libbfio_memory_range_initialize(
63
2.96k
       &file_io_handle,
64
2.96k
       NULL ) != 1 )
65
0
  {
66
0
    return( 0 );
67
0
  }
68
2.96k
  if( libbfio_memory_range_set(
69
2.96k
       file_io_handle,
70
2.96k
       (uint8_t *) data,
71
2.96k
       size,
72
2.96k
       NULL ) != 1 )
73
0
  {
74
0
    goto on_error_libbfio;
75
0
  }
76
2.96k
  if( libpff_file_initialize(
77
2.96k
       &file,
78
2.96k
       NULL ) != 1 )
79
0
  {
80
0
    goto on_error_libbfio;
81
0
  }
82
2.96k
  if( libpff_file_open_file_io_handle(
83
2.96k
       file,
84
2.96k
       file_io_handle,
85
2.96k
       LIBPFF_OPEN_READ,
86
2.96k
       NULL ) != 1 )
87
1.00k
  {
88
1.00k
    goto on_error_libpff_file;
89
1.00k
  }
90
1.96k
  if( libpff_file_get_root_folder(
91
1.96k
       file,
92
1.96k
       &root_folder,
93
1.96k
       NULL ) == 1 )
94
1.71k
  {
95
1.71k
    if( libpff_folder_get_number_of_sub_folders(
96
1.71k
         root_folder,
97
1.71k
         &number_of_sub_folders,
98
1.71k
         NULL ) != 1 )
99
1.46k
    {
100
1.46k
      goto on_error_libpff_root_folder;
101
1.46k
    }
102
257
    if( libpff_folder_get_number_of_sub_messages(
103
257
         root_folder,
104
257
         &number_of_sub_messages,
105
257
         NULL ) != 1 )
106
34
    {
107
34
      goto on_error_libpff_root_folder;
108
34
    }
109
223
    if( libpff_folder_get_number_of_sub_associated_contents(
110
223
         root_folder,
111
223
         &number_of_sub_associated_contents,
112
223
         NULL ) != 1 )
113
29
    {
114
29
      goto on_error_libpff_root_folder;
115
29
    }
116
194
    if( number_of_sub_folders > 0 )
117
187
    {
118
187
      if( libpff_folder_get_sub_folder(
119
187
           root_folder,
120
187
           0,
121
187
           &sub_folder,
122
187
           NULL ) != 1 )
123
51
      {
124
51
        goto on_error_libpff_root_folder;
125
51
      }
126
136
      string_size = 0;
127
128
136
      if( libpff_folder_get_utf8_name_size(
129
136
           sub_folder,
130
136
           &string_size,
131
136
           NULL ) != 1 )
132
69
      {
133
69
        goto on_error_libpff_sub_folder;
134
69
      }
135
67
      if( libpff_folder_get_utf8_name(
136
67
           sub_folder,
137
67
           (uint8_t *) string,
138
67
           64,
139
67
           NULL ) != 1 )
140
0
      {
141
0
        goto on_error_libpff_sub_folder;
142
0
      }
143
67
      string_size = 0;
144
145
67
      if( libpff_folder_get_utf16_name_size(
146
67
           sub_folder,
147
67
           &string_size,
148
67
           NULL ) != 1 )
149
0
      {
150
0
        goto on_error_libpff_sub_folder;
151
0
      }
152
67
      if( libpff_folder_get_utf16_name(
153
67
           sub_folder,
154
67
           unicode_string,
155
67
           32,
156
67
           NULL ) != 1 )
157
0
      {
158
0
        goto on_error_libpff_sub_folder;
159
0
      }
160
67
      if( libpff_folder_get_number_of_sub_folders(
161
67
           sub_folder,
162
67
           &number_of_sub_folders,
163
67
           NULL ) != 1 )
164
20
      {
165
20
        goto on_error_libpff_sub_folder;
166
20
      }
167
47
      if( libpff_folder_get_number_of_sub_messages(
168
47
           sub_folder,
169
47
           &number_of_sub_messages,
170
47
           NULL ) != 1 )
171
17
      {
172
17
        goto on_error_libpff_sub_folder;
173
17
      }
174
136
 on_error_libpff_sub_folder:
175
136
      libpff_item_free(
176
136
       &sub_folder,
177
136
       NULL );
178
136
    }
179
1.71k
 on_error_libpff_root_folder:
180
1.71k
    libpff_item_free(
181
1.71k
     &root_folder,
182
1.71k
     NULL );
183
1.71k
  }
184
1.96k
  libpff_file_close(
185
1.96k
   file,
186
1.96k
   NULL );
187
188
2.96k
on_error_libpff_file:
189
2.96k
  libpff_file_free(
190
2.96k
   &file,
191
2.96k
   NULL );
192
193
2.96k
on_error_libbfio:
194
2.96k
  libbfio_handle_free(
195
2.96k
   &file_io_handle,
196
2.96k
   NULL );
197
198
2.96k
  return( 0 );
199
2.96k
}
200
201
} /* extern "C" */