Coverage Report

Created: 2026-10-22 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvsapm/ossfuzz/partition_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvsapm partition type
3
 *
4
 * Copyright (C) 2009-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_libvsapm.h"
31
32
#if !defined( LIBVSAPM_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
LIBVSAPM_EXTERN \
38
int libvsapm_volume_open_file_io_handle(
39
     libvsapm_volume_t *volume,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libvsapm_error_t **error );
43
44
#endif /* !defined( LIBVSAPM_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
532
{
50
532
  uint8_t buffer[ 512 ];
51
532
  char string[ 64 ];
52
53
532
  libbfio_handle_t *file_io_handle = NULL;
54
532
  libvsapm_partition_t *partition  = NULL;
55
532
  libvsapm_volume_t *volume        = NULL;
56
532
  off64_t partition_offset         = 0;
57
532
  off64_t volume_offset            = 0;
58
532
  size64_t partition_size          = 0;
59
532
  uint32_t value_32bit             = 0;
60
532
  int number_of_partitions         = 0;
61
532
  int read_iterator                = 0;
62
63
532
  if( libbfio_memory_range_initialize(
64
532
       &file_io_handle,
65
532
       NULL ) != 1 )
66
0
  {
67
0
    return( 0 );
68
0
  }
69
532
  if( libbfio_memory_range_set(
70
532
       file_io_handle,
71
532
       (uint8_t *) data,
72
532
       size,
73
532
       NULL ) != 1 )
74
0
  {
75
0
    goto on_error_libbfio;
76
0
  }
77
532
  if( libvsapm_volume_initialize(
78
532
       &volume,
79
532
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
532
  if( libvsapm_volume_open_file_io_handle(
84
532
       volume,
85
532
       file_io_handle,
86
532
       LIBVSAPM_OPEN_READ,
87
532
       NULL ) != 1 )
88
312
  {
89
312
    goto on_error_libvsapm_volume;
90
312
  }
91
220
  if( libvsapm_volume_get_number_of_partitions(
92
220
       volume,
93
220
       &number_of_partitions,
94
220
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libvsapm_volume;
97
0
  }
98
220
  if( number_of_partitions > 0 )
99
218
  {
100
218
    if( libvsapm_volume_get_partition_by_index(
101
218
         volume,
102
218
         0,
103
218
         &partition,
104
218
         NULL ) != 1 )
105
0
    {
106
0
      goto on_error_libvsapm_volume;
107
0
    }
108
218
    if( libvsapm_partition_get_name_string(
109
218
         partition,
110
218
         string,
111
218
         64,
112
218
         NULL ) != 1 )
113
0
    {
114
0
      goto on_error_libvsapm_partition;
115
0
    }
116
218
    if( libvsapm_partition_get_type_string(
117
218
         partition,
118
218
         string,
119
218
         64,
120
218
         NULL ) != 1 )
121
0
    {
122
0
      goto on_error_libvsapm_partition;
123
0
    }
124
218
    if( libvsapm_partition_get_status_flags(
125
218
         partition,
126
218
         &value_32bit,
127
218
         NULL ) != 1 )
128
0
    {
129
0
      goto on_error_libvsapm_partition;
130
0
    }
131
218
    if( libvsapm_partition_get_volume_offset(
132
218
         partition,
133
218
         &volume_offset,
134
218
         NULL ) != 1 )
135
0
    {
136
0
      goto on_error_libvsapm_partition;
137
0
    }
138
218
    if( libvsapm_partition_get_size(
139
218
         partition,
140
218
         &partition_size,
141
218
         NULL ) != 1 )
142
0
    {
143
0
      goto on_error_libvsapm_partition;
144
0
    }
145
218
    for( read_iterator = 0;
146
5.07k
         read_iterator < 128;
147
4.85k
         read_iterator++ )
148
5.06k
    {
149
5.06k
      if( partition_offset >= partition_size )
150
21
      {
151
21
        break;
152
21
      }
153
5.04k
      if( libvsapm_partition_read_buffer_at_offset(
154
5.04k
           partition,
155
5.04k
           buffer,
156
5.04k
           497,
157
5.04k
           partition_offset,
158
5.04k
           NULL ) == -1 )
159
191
      {
160
191
        goto on_error_libvsapm_partition;
161
191
      }
162
4.85k
      partition_offset += 497;
163
4.85k
    }
164
218
on_error_libvsapm_partition:
165
218
    libvsapm_partition_free(
166
218
     &partition,
167
218
     NULL );
168
218
  }
169
220
  libvsapm_volume_close(
170
220
   volume,
171
220
   NULL );
172
173
532
on_error_libvsapm_volume:
174
532
  libvsapm_volume_free(
175
532
   &volume,
176
532
   NULL );
177
178
532
on_error_libbfio:
179
532
  libbfio_handle_free(
180
532
   &file_io_handle,
181
532
   NULL );
182
183
532
  return( 0 );
184
532
}
185
186
} /* extern "C" */
187