Coverage Report

Created: 2026-08-31 07:43

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