Coverage Report

Created: 2026-07-10 07:32

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
487
{
50
487
  uint8_t buffer[ 512 ];
51
487
  char string[ 64 ];
52
53
487
  libbfio_handle_t *file_io_handle = NULL;
54
487
  libvsapm_partition_t *partition  = NULL;
55
487
  libvsapm_volume_t *volume        = NULL;
56
487
  off64_t partition_offset         = 0;
57
487
  off64_t volume_offset            = 0;
58
487
  size64_t partition_size          = 0;
59
487
  uint32_t value_32bit             = 0;
60
487
  int number_of_partitions         = 0;
61
487
  int read_iterator                = 0;
62
63
487
  if( libbfio_memory_range_initialize(
64
487
       &file_io_handle,
65
487
       NULL ) != 1 )
66
0
  {
67
0
    return( 0 );
68
0
  }
69
487
  if( libbfio_memory_range_set(
70
487
       file_io_handle,
71
487
       (uint8_t *) data,
72
487
       size,
73
487
       NULL ) != 1 )
74
0
  {
75
0
    goto on_error_libbfio;
76
0
  }
77
487
  if( libvsapm_volume_initialize(
78
487
       &volume,
79
487
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
487
  if( libvsapm_volume_open_file_io_handle(
84
487
       volume,
85
487
       file_io_handle,
86
487
       LIBVSAPM_OPEN_READ,
87
487
       NULL ) != 1 )
88
287
  {
89
287
    goto on_error_libvsapm_volume;
90
287
  }
91
200
  if( libvsapm_volume_get_number_of_partitions(
92
200
       volume,
93
200
       &number_of_partitions,
94
200
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libvsapm_volume;
97
0
  }
98
200
  if( number_of_partitions > 0 )
99
199
  {
100
199
    if( libvsapm_volume_get_partition_by_index(
101
199
         volume,
102
199
         0,
103
199
         &partition,
104
199
         NULL ) != 1 )
105
0
    {
106
0
      goto on_error_libvsapm_volume;
107
0
    }
108
199
    if( libvsapm_partition_get_name_string(
109
199
         partition,
110
199
         string,
111
199
         64,
112
199
         NULL ) != 1 )
113
0
    {
114
0
      goto on_error_libvsapm_partition;
115
0
    }
116
199
    if( libvsapm_partition_get_type_string(
117
199
         partition,
118
199
         string,
119
199
         64,
120
199
         NULL ) != 1 )
121
0
    {
122
0
      goto on_error_libvsapm_partition;
123
0
    }
124
199
    if( libvsapm_partition_get_status_flags(
125
199
         partition,
126
199
         &value_32bit,
127
199
         NULL ) != 1 )
128
0
    {
129
0
      goto on_error_libvsapm_partition;
130
0
    }
131
199
    if( libvsapm_partition_get_volume_offset(
132
199
         partition,
133
199
         &volume_offset,
134
199
         NULL ) != 1 )
135
0
    {
136
0
      goto on_error_libvsapm_partition;
137
0
    }
138
199
    if( libvsapm_partition_get_size(
139
199
         partition,
140
199
         &partition_size,
141
199
         NULL ) != 1 )
142
0
    {
143
0
      goto on_error_libvsapm_partition;
144
0
    }
145
199
    for( read_iterator = 0;
146
4.07k
         read_iterator < 128;
147
3.87k
         read_iterator++ )
148
4.07k
    {
149
4.07k
      if( partition_offset >= partition_size )
150
18
      {
151
18
        break;
152
18
      }
153
4.05k
      if( libvsapm_partition_read_buffer_at_offset(
154
4.05k
           partition,
155
4.05k
           buffer,
156
4.05k
           497,
157
4.05k
           partition_offset,
158
4.05k
           NULL ) == -1 )
159
180
      {
160
180
        goto on_error_libvsapm_partition;
161
180
      }
162
3.87k
      partition_offset += 497;
163
3.87k
    }
164
199
on_error_libvsapm_partition:
165
199
    libvsapm_partition_free(
166
199
     &partition,
167
199
     NULL );
168
199
  }
169
200
  libvsapm_volume_close(
170
200
   volume,
171
200
   NULL );
172
173
487
on_error_libvsapm_volume:
174
487
  libvsapm_volume_free(
175
487
   &volume,
176
487
   NULL );
177
178
487
on_error_libbfio:
179
487
  libbfio_handle_free(
180
487
   &file_io_handle,
181
487
   NULL );
182
183
487
  return( 0 );
184
487
}
185
186
} /* extern "C" */
187