Coverage Report

Created: 2026-12-01 07:31

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
515
{
50
515
  uint8_t buffer[ 512 ];
51
515
  char string[ 64 ];
52
53
515
  libbfio_handle_t *file_io_handle = NULL;
54
515
  libvsapm_partition_t *partition  = NULL;
55
515
  libvsapm_volume_t *volume        = NULL;
56
515
  off64_t partition_offset         = 0;
57
515
  off64_t volume_offset            = 0;
58
515
  size64_t partition_size          = 0;
59
515
  uint32_t value_32bit             = 0;
60
515
  int number_of_partitions         = 0;
61
515
  int read_iterator                = 0;
62
63
515
  if( libbfio_memory_range_initialize(
64
515
       &file_io_handle,
65
515
       NULL ) != 1 )
66
0
  {
67
0
    return( 0 );
68
0
  }
69
515
  if( libbfio_memory_range_set(
70
515
       file_io_handle,
71
515
       (uint8_t *) data,
72
515
       size,
73
515
       NULL ) != 1 )
74
0
  {
75
0
    goto on_error_libbfio;
76
0
  }
77
515
  if( libvsapm_volume_initialize(
78
515
       &volume,
79
515
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
515
  if( libvsapm_volume_open_file_io_handle(
84
515
       volume,
85
515
       file_io_handle,
86
515
       LIBVSAPM_OPEN_READ,
87
515
       NULL ) != 1 )
88
307
  {
89
307
    goto on_error_libvsapm_volume;
90
307
  }
91
208
  if( libvsapm_volume_get_number_of_partitions(
92
208
       volume,
93
208
       &number_of_partitions,
94
208
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libvsapm_volume;
97
0
  }
98
208
  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.79k
         read_iterator < 128;
147
4.58k
         read_iterator++ )
148
4.79k
    {
149
4.79k
      if( partition_offset >= partition_size )
150
23
      {
151
23
        break;
152
23
      }
153
4.76k
      if( libvsapm_partition_read_buffer_at_offset(
154
4.76k
           partition,
155
4.76k
           buffer,
156
4.76k
           497,
157
4.76k
           partition_offset,
158
4.76k
           NULL ) == -1 )
159
179
      {
160
179
        goto on_error_libvsapm_partition;
161
179
      }
162
4.58k
      partition_offset += 497;
163
4.58k
    }
164
206
on_error_libvsapm_partition:
165
206
    libvsapm_partition_free(
166
206
     &partition,
167
206
     NULL );
168
206
  }
169
208
  libvsapm_volume_close(
170
208
   volume,
171
208
   NULL );
172
173
515
on_error_libvsapm_volume:
174
515
  libvsapm_volume_free(
175
515
   &volume,
176
515
   NULL );
177
178
515
on_error_libbfio:
179
515
  libbfio_handle_free(
180
515
   &file_io_handle,
181
515
   NULL );
182
183
515
  return( 0 );
184
515
}
185
186
} /* extern "C" */
187