Coverage Report

Created: 2026-10-22 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libqcow/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libqcow file type
3
 *
4
 * Copyright (C) 2010-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_libqcow.h"
31
32
#if !defined( LIBQCOW_HAVE_BFIO )
33
34
/* Opens a file using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBQCOW_EXTERN \
38
int libqcow_file_open_file_io_handle(
39
     libqcow_file_t *file,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libqcow_error_t **error );
43
44
#endif /* !defined( LIBQCOW_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
1.80k
{
50
1.80k
  uint8_t buffer[ 512 ];
51
1.80k
  uint8_t utf8_string[ 64 ];
52
1.80k
  uint16_t utf16_string[ 64 ];
53
54
1.80k
  libbfio_handle_t *file_io_handle = NULL;
55
1.80k
  libqcow_file_t *file             = NULL;
56
1.80k
  off64_t media_offset             = 0;
57
1.80k
  size64_t media_size              = 0;
58
1.80k
  size_t string_size               = 0;
59
1.80k
  uint32_t value_32bit             = 0;
60
1.80k
  int number_of_snapshots          = 0;
61
1.80k
  int read_iterator                = 0;
62
63
1.80k
  if( libbfio_memory_range_initialize(
64
1.80k
       &file_io_handle,
65
1.80k
       NULL ) != 1 )
66
0
  {
67
0
    return( 0 );
68
0
  }
69
1.80k
  if( libbfio_memory_range_set(
70
1.80k
       file_io_handle,
71
1.80k
       (uint8_t *) data,
72
1.80k
       size,
73
1.80k
       NULL ) != 1 )
74
0
  {
75
0
    goto on_error_libbfio;
76
0
  }
77
1.80k
  if( libqcow_file_initialize(
78
1.80k
       &file,
79
1.80k
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
1.80k
  if( libqcow_file_open_file_io_handle(
84
1.80k
       file,
85
1.80k
       file_io_handle,
86
1.80k
       LIBQCOW_OPEN_READ,
87
1.80k
       NULL ) != 1 )
88
818
  {
89
818
    goto on_error_libqcow;
90
818
  }
91
989
  if( libqcow_file_get_format_version(
92
989
       file,
93
989
       &value_32bit,
94
989
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libqcow;
97
0
  }
98
989
  if( libqcow_file_get_encryption_method(
99
989
       file,
100
989
       &value_32bit,
101
989
       NULL ) != 1 )
102
0
  {
103
0
    goto on_error_libqcow;
104
0
  }
105
989
  if( libqcow_file_get_utf8_backing_filename_size(
106
989
       file,
107
989
       &string_size,
108
989
       NULL ) == -1 )
109
64
  {
110
64
    goto on_error_libqcow;
111
64
  }
112
925
  if( libqcow_file_get_utf8_backing_filename(
113
925
       file,
114
925
       utf8_string,
115
925
       64,
116
925
       NULL ) == -1 )
117
18
  {
118
18
    goto on_error_libqcow;
119
18
  }
120
907
  if( libqcow_file_get_utf16_backing_filename_size(
121
907
       file,
122
907
       &string_size,
123
907
       NULL ) == -1 )
124
0
  {
125
0
    goto on_error_libqcow;
126
0
  }
127
907
  if( libqcow_file_get_utf16_backing_filename(
128
907
       file,
129
907
       utf16_string,
130
907
       64,
131
907
       NULL ) == -1 )
132
0
  {
133
0
    goto on_error_libqcow;
134
0
  }
135
907
  if( libqcow_file_get_number_of_snapshots(
136
907
       file,
137
907
       &number_of_snapshots,
138
907
       NULL ) != 1 )
139
0
  {
140
0
    goto on_error_libqcow;
141
0
  }
142
907
  if( libqcow_file_get_media_size(
143
907
       file,
144
907
       &media_size,
145
907
       NULL ) != 1 )
146
0
  {
147
0
    goto on_error_libqcow;
148
0
  }
149
907
  for( read_iterator = 0;
150
14.4k
       read_iterator < 128;
151
13.5k
       read_iterator++ )
152
14.4k
  {
153
14.4k
    if( media_offset >= media_size )
154
34
    {
155
34
      break;
156
34
    }
157
14.3k
    if( libqcow_file_read_buffer_at_offset(
158
14.3k
         file,
159
14.3k
         buffer,
160
14.3k
         497,
161
14.3k
         media_offset,
162
14.3k
         NULL ) == -1 )
163
851
    {
164
851
      goto on_error_libqcow;
165
851
    }
166
13.5k
    media_offset += 497;
167
13.5k
  }
168
56
  libqcow_file_close(
169
56
   file,
170
56
   NULL );
171
172
1.80k
on_error_libqcow:
173
1.80k
  libqcow_file_free(
174
1.80k
   &file,
175
1.80k
   NULL );
176
177
1.80k
on_error_libbfio:
178
1.80k
  libbfio_handle_free(
179
1.80k
   &file_io_handle,
180
1.80k
   NULL );
181
182
1.80k
  return( 0 );
183
1.80k
}
184
185
} /* extern "C" */
186