Coverage Report

Created: 2026-08-31 07:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvsmbr/ossfuzz/partition_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvsmbr partition type
3
 *
4
 * Copyright (C) 2019-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_libvsmbr.h"
31
32
#if !defined( LIBVSMBR_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
LIBVSMBR_EXTERN \
38
int libvsmbr_volume_open_file_io_handle(
39
     libvsmbr_volume_t *volume,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libvsmbr_error_t **error );
43
44
#endif /* !defined( LIBVSMBR_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
352
{
50
352
  uint8_t buffer[ 512 ];
51
52
352
  libbfio_handle_t *file_io_handle = NULL;
53
352
  libvsmbr_partition_t *partition  = NULL;
54
352
  libvsmbr_volume_t *volume        = NULL;
55
352
  off64_t partition_offset         = 0;
56
352
  off64_t volume_offset            = 0;
57
352
  size64_t partition_size          = 0;
58
352
  uint8_t value_8bit               = 0;
59
352
  int number_of_partitions         = 0;
60
352
  int read_iterator                = 0;
61
62
352
  if( libbfio_memory_range_initialize(
63
352
       &file_io_handle,
64
352
       NULL ) != 1 )
65
0
  {
66
0
    return( 0 );
67
0
  }
68
352
  if( libbfio_memory_range_set(
69
352
       file_io_handle,
70
352
       (uint8_t *) data,
71
352
       size,
72
352
       NULL ) != 1 )
73
0
  {
74
0
    goto on_error_libbfio;
75
0
  }
76
352
  if( libvsmbr_volume_initialize(
77
352
       &volume,
78
352
       NULL ) != 1 )
79
0
  {
80
0
    goto on_error_libbfio;
81
0
  }
82
352
  if( libvsmbr_volume_open_file_io_handle(
83
352
       volume,
84
352
       file_io_handle,
85
352
       LIBVSMBR_OPEN_READ,
86
352
       NULL ) != 1 )
87
144
  {
88
144
    goto on_error_libvsmbr_volume;
89
144
  }
90
208
  if( libvsmbr_volume_get_number_of_partitions(
91
208
       volume,
92
208
       &number_of_partitions,
93
208
       NULL ) != 1 )
94
0
  {
95
0
    goto on_error_libvsmbr_volume;
96
0
  }
97
208
  if( number_of_partitions > 0 )
98
206
  {
99
206
    if( libvsmbr_volume_get_partition_by_index(
100
206
         volume,
101
206
         0,
102
206
         &partition,
103
206
         NULL ) != 1 )
104
0
    {
105
0
      goto on_error_libvsmbr_volume;
106
0
    }
107
206
    if( libvsmbr_partition_get_type(
108
206
         partition,
109
206
         &value_8bit,
110
206
         NULL ) != 1 )
111
0
    {
112
0
      goto on_error_libvsmbr_partition;
113
0
    }
114
206
    if( libvsmbr_partition_get_volume_offset(
115
206
         partition,
116
206
         &volume_offset,
117
206
         NULL ) != 1 )
118
0
    {
119
0
      goto on_error_libvsmbr_partition;
120
0
    }
121
206
    if( libvsmbr_partition_get_size(
122
206
         partition,
123
206
         &partition_size,
124
206
         NULL ) != 1 )
125
0
    {
126
0
      goto on_error_libvsmbr_partition;
127
0
    }
128
206
    for( read_iterator = 0;
129
5.10k
         read_iterator < 128;
130
4.90k
         read_iterator++ )
131
5.10k
    {
132
5.10k
      if( partition_offset >= partition_size )
133
21
      {
134
21
        break;
135
21
      }
136
5.08k
      if( libvsmbr_partition_read_buffer_at_offset(
137
5.08k
           partition,
138
5.08k
           buffer,
139
5.08k
           497,
140
5.08k
           partition_offset,
141
5.08k
           NULL ) == -1 )
142
180
      {
143
180
        goto on_error_libvsmbr_partition;
144
180
      }
145
4.90k
      partition_offset += 497;
146
4.90k
    }
147
206
on_error_libvsmbr_partition:
148
206
    libvsmbr_partition_free(
149
206
     &partition,
150
206
     NULL );
151
206
  }
152
208
  libvsmbr_volume_close(
153
208
   volume,
154
208
   NULL );
155
156
352
on_error_libvsmbr_volume:
157
352
  libvsmbr_volume_free(
158
352
   &volume,
159
352
   NULL );
160
161
352
on_error_libbfio:
162
352
  libbfio_handle_free(
163
352
   &file_io_handle,
164
352
   NULL );
165
166
352
  return( 0 );
167
352
}
168
169
} /* extern "C" */
170