Coverage Report

Created: 2026-07-25 07:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvsbsdl/ossfuzz/partition_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvsbsdl partition type
3
 *
4
 * Copyright (C) 2023-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_libvsbsdl.h"
31
32
#if !defined( LIBVSBSDL_HAVE_BFIO )
33
34
/* Opens a volume using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBVSBSDL_EXTERN \
38
int libvsbsdl_volume_open_file_io_handle(
39
     libvsbsdl_volume_t *volume,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libvsbsdl_error_t **error );
43
44
#endif /* !defined( LIBVSBSDL_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
473
{
50
473
  uint8_t buffer[ 512 ];
51
473
  char string[ 64 ];
52
53
473
  libbfio_handle_t *file_io_handle = NULL;
54
473
  libvsbsdl_partition_t *partition = NULL;
55
473
  libvsbsdl_volume_t *volume       = NULL;
56
473
  off64_t partition_offset         = 0;
57
473
  off64_t volume_offset            = 0;
58
473
  size64_t partition_size          = 0;
59
473
  uint16_t value_16bit             = 0;
60
473
  int number_of_partitions         = 0;
61
473
  int read_iterator                = 0;
62
63
473
  if( libbfio_memory_range_initialize(
64
473
       &file_io_handle,
65
473
       NULL ) != 1 )
66
0
  {
67
0
    return( 0 );
68
0
  }
69
473
  if( libbfio_memory_range_set(
70
473
       file_io_handle,
71
473
       (uint8_t *) data,
72
473
       size,
73
473
       NULL ) != 1 )
74
0
  {
75
0
    goto on_error_libbfio;
76
0
  }
77
473
  if( libvsbsdl_volume_initialize(
78
473
       &volume,
79
473
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
473
  if( libvsbsdl_volume_open_file_io_handle(
84
473
       volume,
85
473
       file_io_handle,
86
473
       LIBVSBSDL_OPEN_READ,
87
473
       NULL ) != 1 )
88
98
  {
89
98
    goto on_error_libvsbsdl_volume;
90
98
  }
91
375
  if( libvsbsdl_volume_get_number_of_partitions(
92
375
       volume,
93
375
       &number_of_partitions,
94
375
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libvsbsdl_volume;
97
0
  }
98
375
  if( number_of_partitions > 0 )
99
374
  {
100
374
    if( libvsbsdl_volume_get_partition_by_index(
101
374
         volume,
102
374
         0,
103
374
         &partition,
104
374
         NULL ) != 1 )
105
66
    {
106
66
      goto on_error_libvsbsdl_volume;
107
66
    }
108
308
    if( libvsbsdl_partition_get_entry_index(
109
308
         partition,
110
308
         &value_16bit,
111
308
         NULL ) != 1 )
112
0
    {
113
0
      goto on_error_libvsbsdl_partition;
114
0
    }
115
308
    if( libvsbsdl_partition_get_name_string(
116
308
         partition,
117
308
         string,
118
308
         64,
119
308
         NULL ) != 1 )
120
0
    {
121
0
      goto on_error_libvsbsdl_partition;
122
0
    }
123
308
    if( libvsbsdl_partition_get_volume_offset(
124
308
         partition,
125
308
         &volume_offset,
126
308
         NULL ) != 1 )
127
0
    {
128
0
      goto on_error_libvsbsdl_partition;
129
0
    }
130
308
    if( libvsbsdl_partition_get_size(
131
308
         partition,
132
308
         &partition_size,
133
308
         NULL ) != 1 )
134
0
    {
135
0
      goto on_error_libvsbsdl_partition;
136
0
    }
137
308
    for( read_iterator = 0;
138
4.27k
         read_iterator < 128;
139
3.96k
         read_iterator++ )
140
4.25k
    {
141
4.25k
      if( partition_offset >= partition_size )
142
20
      {
143
20
        break;
144
20
      }
145
4.23k
      if( libvsbsdl_partition_read_buffer_at_offset(
146
4.23k
           partition,
147
4.23k
           buffer,
148
4.23k
           497,
149
4.23k
           partition_offset,
150
4.23k
           NULL ) == -1 )
151
273
      {
152
273
        goto on_error_libvsbsdl_partition;
153
273
      }
154
3.96k
      partition_offset += 497;
155
3.96k
    }
156
308
on_error_libvsbsdl_partition:
157
308
    libvsbsdl_partition_free(
158
308
     &partition,
159
308
     NULL );
160
308
  }
161
309
  libvsbsdl_volume_close(
162
309
   volume,
163
309
   NULL );
164
165
473
on_error_libvsbsdl_volume:
166
473
  libvsbsdl_volume_free(
167
473
   &volume,
168
473
   NULL );
169
170
473
on_error_libbfio:
171
473
  libbfio_handle_free(
172
473
   &file_io_handle,
173
473
   NULL );
174
175
473
  return( 0 );
176
473
}
177
178
} /* extern "C" */
179