Coverage Report

Created: 2026-12-01 07:31

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
460
{
50
460
  uint8_t buffer[ 512 ];
51
460
  char string[ 64 ];
52
53
460
  libbfio_handle_t *file_io_handle = NULL;
54
460
  libvsbsdl_partition_t *partition = NULL;
55
460
  libvsbsdl_volume_t *volume       = NULL;
56
460
  off64_t partition_offset         = 0;
57
460
  off64_t volume_offset            = 0;
58
460
  size64_t partition_size          = 0;
59
460
  uint16_t value_16bit             = 0;
60
460
  int number_of_partitions         = 0;
61
460
  int read_iterator                = 0;
62
63
460
  if( libbfio_memory_range_initialize(
64
460
       &file_io_handle,
65
460
       NULL ) != 1 )
66
0
  {
67
0
    return( 0 );
68
0
  }
69
460
  if( libbfio_memory_range_set(
70
460
       file_io_handle,
71
460
       (uint8_t *) data,
72
460
       size,
73
460
       NULL ) != 1 )
74
0
  {
75
0
    goto on_error_libbfio;
76
0
  }
77
460
  if( libvsbsdl_volume_initialize(
78
460
       &volume,
79
460
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
460
  if( libvsbsdl_volume_open_file_io_handle(
84
460
       volume,
85
460
       file_io_handle,
86
460
       LIBVSBSDL_OPEN_READ,
87
460
       NULL ) != 1 )
88
104
  {
89
104
    goto on_error_libvsbsdl_volume;
90
104
  }
91
356
  if( libvsbsdl_volume_get_number_of_partitions(
92
356
       volume,
93
356
       &number_of_partitions,
94
356
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libvsbsdl_volume;
97
0
  }
98
356
  if( number_of_partitions > 0 )
99
355
  {
100
355
    if( libvsbsdl_volume_get_partition_by_index(
101
355
         volume,
102
355
         0,
103
355
         &partition,
104
355
         NULL ) != 1 )
105
66
    {
106
66
      goto on_error_libvsbsdl_volume;
107
66
    }
108
289
    if( libvsbsdl_partition_get_entry_index(
109
289
         partition,
110
289
         &value_16bit,
111
289
         NULL ) != 1 )
112
0
    {
113
0
      goto on_error_libvsbsdl_partition;
114
0
    }
115
289
    if( libvsbsdl_partition_get_name_string(
116
289
         partition,
117
289
         string,
118
289
         64,
119
289
         NULL ) != 1 )
120
0
    {
121
0
      goto on_error_libvsbsdl_partition;
122
0
    }
123
289
    if( libvsbsdl_partition_get_volume_offset(
124
289
         partition,
125
289
         &volume_offset,
126
289
         NULL ) != 1 )
127
0
    {
128
0
      goto on_error_libvsbsdl_partition;
129
0
    }
130
289
    if( libvsbsdl_partition_get_size(
131
289
         partition,
132
289
         &partition_size,
133
289
         NULL ) != 1 )
134
0
    {
135
0
      goto on_error_libvsbsdl_partition;
136
0
    }
137
289
    for( read_iterator = 0;
138
4.63k
         read_iterator < 128;
139
4.34k
         read_iterator++ )
140
4.61k
    {
141
4.61k
      if( partition_offset >= partition_size )
142
23
      {
143
23
        break;
144
23
      }
145
4.59k
      if( libvsbsdl_partition_read_buffer_at_offset(
146
4.59k
           partition,
147
4.59k
           buffer,
148
4.59k
           497,
149
4.59k
           partition_offset,
150
4.59k
           NULL ) == -1 )
151
249
      {
152
249
        goto on_error_libvsbsdl_partition;
153
249
      }
154
4.34k
      partition_offset += 497;
155
4.34k
    }
156
289
on_error_libvsbsdl_partition:
157
289
    libvsbsdl_partition_free(
158
289
     &partition,
159
289
     NULL );
160
289
  }
161
290
  libvsbsdl_volume_close(
162
290
   volume,
163
290
   NULL );
164
165
460
on_error_libvsbsdl_volume:
166
460
  libvsbsdl_volume_free(
167
460
   &volume,
168
460
   NULL );
169
170
460
on_error_libbfio:
171
460
  libbfio_handle_free(
172
460
   &file_io_handle,
173
460
   NULL );
174
175
460
  return( 0 );
176
460
}
177
178
} /* extern "C" */
179