Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libolecf/ossfuzz/stream_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libolecf stream 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
#include <stdlib.h>
26
27
/* Note that some of the OSS-Fuzz engines use C++
28
 */
29
extern "C" {
30
31
#include "ossfuzz_libbfio.h"
32
#include "ossfuzz_libolecf.h"
33
34
#if !defined( LIBOLECF_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
LIBOLECF_EXTERN \
40
int libolecf_file_open_file_io_handle(
41
      libolecf_file_t *file,
42
      libbfio_handle_t *file_io_handle,
43
      int access_flags,
44
      libolecf_error_t **error );
45
46
#endif /* !defined( LIBOLECF_HAVE_BFIO ) */
47
48
/* Limits the size of the data buffer
49
 */
50
2.88k
#define FUZZER_DATA_SIZE_LIMIT  ( (size_t) 0x10000 )
51
52
/* Limits the number of sub items to walk to find a stream type item
53
 */
54
1.28k
#define FUZZER_SUB_ITEM_LIMIT 8
55
56
int LLVMFuzzerTestOneInput(
57
      const uint8_t *data,
58
      size_t size )
59
1.50k
{
60
1.50k
  uint8_t *buffer    = NULL;
61
1.50k
  off64_t offset     = 0;
62
1.50k
  ssize_t read_count = 0;
63
64
1.50k
  libbfio_handle_t *file_io_handle = NULL;
65
1.50k
  libolecf_file_t *file            = NULL;
66
1.50k
  libolecf_item_t *item            = NULL;
67
1.50k
  libolecf_item_t *stream          = NULL;
68
1.50k
  uint8_t item_type                = 0;
69
1.50k
  int number_of_sub_items         = 0;
70
1.50k
  int result                      = 0;
71
1.50k
  int sub_item_index              = 0;
72
73
1.50k
  if( libbfio_memory_range_initialize(
74
1.50k
       &file_io_handle,
75
1.50k
       NULL ) != 1 )
76
0
  {
77
0
    return( 0 );
78
0
  }
79
1.50k
  if( libbfio_memory_range_set(
80
1.50k
       file_io_handle,
81
1.50k
       (uint8_t *) data,
82
1.50k
       size,
83
1.50k
       NULL ) != 1 )
84
0
  {
85
0
    goto on_error_libbfio;
86
0
  }
87
1.50k
  if( libolecf_file_initialize(
88
1.50k
       &file,
89
1.50k
       NULL ) != 1 )
90
0
  {
91
0
    goto on_error_libbfio;
92
0
  }
93
1.50k
  if( libolecf_file_open_file_io_handle(
94
1.50k
       file,
95
1.50k
       file_io_handle,
96
1.50k
       LIBOLECF_OPEN_READ,
97
1.50k
       NULL ) != 1 )
98
996
  {
99
996
    goto on_error_libolecf_file;
100
996
  }
101
506
  if( libolecf_file_get_root_item(
102
506
       file,
103
506
       &item,
104
506
       NULL ) == 1 )
105
465
  {
106
465
    buffer = (uint8_t *) malloc( FUZZER_DATA_SIZE_LIMIT );
107
108
465
    if( buffer != NULL )
109
465
    {
110
      /* Walk the sub items to find a stream type item
111
       */
112
465
      if( libolecf_item_get_number_of_sub_items(
113
465
           item,
114
465
           &number_of_sub_items,
115
465
           NULL ) == 1 )
116
465
      {
117
465
        for( sub_item_index = 0;
118
1.73k
             sub_item_index < number_of_sub_items;
119
1.27k
             sub_item_index++ )
120
1.28k
        {
121
1.28k
          item_type = 0;
122
123
1.28k
          if( sub_item_index >= FUZZER_SUB_ITEM_LIMIT )
124
15
          {
125
15
            break;
126
15
          }
127
1.27k
          if( libolecf_item_get_sub_item(
128
1.27k
               item,
129
1.27k
               sub_item_index,
130
1.27k
               &stream,
131
1.27k
               NULL ) != 1 )
132
0
          {
133
0
            break;
134
0
          }
135
1.27k
          result = libolecf_item_get_type(
136
1.27k
                   stream,
137
1.27k
                   &item_type,
138
1.27k
                   NULL );
139
1.27k
          if( ( result == 1 )
140
1.27k
           && ( item_type == LIBOLECF_ITEM_TYPE_STREAM ) )
141
1.11k
          {
142
1.11k
            libolecf_stream_get_offset(
143
1.11k
             stream,
144
1.11k
             &offset,
145
1.11k
             NULL );
146
1.11k
            libolecf_stream_read_buffer(
147
1.11k
             stream,
148
1.11k
             buffer,
149
1.11k
             FUZZER_DATA_SIZE_LIMIT,
150
1.11k
             NULL );
151
1.11k
            libolecf_stream_seek_offset(
152
1.11k
             stream,
153
1.11k
             0,
154
1.11k
             SEEK_SET,
155
1.11k
             NULL );
156
1.11k
            read_count = libolecf_stream_read_buffer_at_offset(
157
1.11k
               stream,
158
1.11k
               buffer,
159
1.11k
               FUZZER_DATA_SIZE_LIMIT,
160
1.11k
               (off64_t) 0,
161
1.11k
               NULL );
162
1.11k
            if( ( read_count > 0 )
163
184
             && ( read_count <= (ssize_t) FUZZER_DATA_SIZE_LIMIT ) )
164
184
            {
165
              /* Use the value as a sink
166
               * to keep it live
167
               */
168
184
              offset = (off64_t) buffer[ 0 ];
169
184
            }
170
            /* Attempt other seek operations
171
             * to trigger the error paths
172
             */
173
1.11k
            libolecf_stream_seek_offset(
174
1.11k
             stream,
175
1.11k
             1,
176
1.11k
             SEEK_CUR,
177
1.11k
             NULL );
178
1.11k
            libolecf_stream_seek_offset(
179
1.11k
             stream,
180
1.11k
             -1,
181
1.11k
             SEEK_END,
182
1.11k
             NULL );
183
1.11k
            libolecf_stream_seek_offset(
184
1.11k
             stream,
185
1.11k
             -1,
186
1.11k
             SEEK_SET,
187
1.11k
             NULL );
188
            /* An invalid whence value should
189
             * trigger an unsupported error
190
             */
191
1.11k
            libolecf_stream_seek_offset(
192
1.11k
             stream,
193
1.11k
             0,
194
1.11k
             8,
195
1.11k
             NULL );
196
1.11k
            libolecf_stream_get_offset(
197
1.11k
             stream,
198
1.11k
             &offset,
199
1.11k
             NULL );
200
1.11k
          }
201
1.27k
          libolecf_item_free(
202
1.27k
           &stream,
203
1.27k
           NULL );
204
1.27k
        }
205
465
      }
206
465
    }
207
465
    free( buffer );
208
465
    buffer = NULL;
209
465
    libolecf_item_free(
210
465
     &item,
211
465
     NULL );
212
465
  }
213
506
  libolecf_file_close(
214
506
   file,
215
506
   NULL );
216
217
1.50k
on_error_libolecf_file:
218
1.50k
  libolecf_file_free(
219
1.50k
   &file,
220
1.50k
   NULL );
221
222
1.50k
on_error_libbfio:
223
1.50k
  libbfio_handle_free(
224
1.50k
   &file_io_handle,
225
1.50k
   NULL );
226
227
1.50k
  return( 0 );
228
1.50k
}
229
230
} /* extern "C" */