Coverage Report

Created: 2026-07-25 07:30

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
354
{
50
354
  uint8_t buffer[ 512 ];
51
52
354
  libbfio_handle_t *file_io_handle = NULL;
53
354
  libvsmbr_partition_t *partition  = NULL;
54
354
  libvsmbr_volume_t *volume        = NULL;
55
354
  off64_t partition_offset         = 0;
56
354
  off64_t volume_offset            = 0;
57
354
  size64_t partition_size          = 0;
58
354
  uint8_t value_8bit               = 0;
59
354
  int number_of_partitions         = 0;
60
354
  int read_iterator                = 0;
61
62
354
  if( libbfio_memory_range_initialize(
63
354
       &file_io_handle,
64
354
       NULL ) != 1 )
65
0
  {
66
0
    return( 0 );
67
0
  }
68
354
  if( libbfio_memory_range_set(
69
354
       file_io_handle,
70
354
       (uint8_t *) data,
71
354
       size,
72
354
       NULL ) != 1 )
73
0
  {
74
0
    goto on_error_libbfio;
75
0
  }
76
354
  if( libvsmbr_volume_initialize(
77
354
       &volume,
78
354
       NULL ) != 1 )
79
0
  {
80
0
    goto on_error_libbfio;
81
0
  }
82
354
  if( libvsmbr_volume_open_file_io_handle(
83
354
       volume,
84
354
       file_io_handle,
85
354
       LIBVSMBR_OPEN_READ,
86
354
       NULL ) != 1 )
87
148
  {
88
148
    goto on_error_libvsmbr_volume;
89
148
  }
90
206
  if( libvsmbr_volume_get_number_of_partitions(
91
206
       volume,
92
206
       &number_of_partitions,
93
206
       NULL ) != 1 )
94
0
  {
95
0
    goto on_error_libvsmbr_volume;
96
0
  }
97
206
  if( number_of_partitions > 0 )
98
203
  {
99
203
    if( libvsmbr_volume_get_partition_by_index(
100
203
         volume,
101
203
         0,
102
203
         &partition,
103
203
         NULL ) != 1 )
104
0
    {
105
0
      goto on_error_libvsmbr_volume;
106
0
    }
107
203
    if( libvsmbr_partition_get_type(
108
203
         partition,
109
203
         &value_8bit,
110
203
         NULL ) != 1 )
111
0
    {
112
0
      goto on_error_libvsmbr_partition;
113
0
    }
114
203
    if( libvsmbr_partition_get_volume_offset(
115
203
         partition,
116
203
         &volume_offset,
117
203
         NULL ) != 1 )
118
0
    {
119
0
      goto on_error_libvsmbr_partition;
120
0
    }
121
203
    if( libvsmbr_partition_get_size(
122
203
         partition,
123
203
         &partition_size,
124
203
         NULL ) != 1 )
125
0
    {
126
0
      goto on_error_libvsmbr_partition;
127
0
    }
128
203
    for( read_iterator = 0;
129
5.14k
         read_iterator < 128;
130
4.93k
         read_iterator++ )
131
5.13k
    {
132
5.13k
      if( partition_offset >= partition_size )
133
23
      {
134
23
        break;
135
23
      }
136
5.11k
      if( libvsmbr_partition_read_buffer_at_offset(
137
5.11k
           partition,
138
5.11k
           buffer,
139
5.11k
           497,
140
5.11k
           partition_offset,
141
5.11k
           NULL ) == -1 )
142
174
      {
143
174
        goto on_error_libvsmbr_partition;
144
174
      }
145
4.93k
      partition_offset += 497;
146
4.93k
    }
147
203
on_error_libvsmbr_partition:
148
203
    libvsmbr_partition_free(
149
203
     &partition,
150
203
     NULL );
151
203
  }
152
206
  libvsmbr_volume_close(
153
206
   volume,
154
206
   NULL );
155
156
354
on_error_libvsmbr_volume:
157
354
  libvsmbr_volume_free(
158
354
   &volume,
159
354
   NULL );
160
161
354
on_error_libbfio:
162
354
  libbfio_handle_free(
163
354
   &file_io_handle,
164
354
   NULL );
165
166
354
  return( 0 );
167
354
}
168
169
} /* extern "C" */
170