Coverage Report

Created: 2026-10-22 07:28

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