Coverage Report

Created: 2026-10-22 07:28

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