Coverage Report

Created: 2026-08-31 07:43

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