Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/ossfuzz/snapshot_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libfsapfs snapshot type
3
 *
4
 * Copyright (C) 2018-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_libfsapfs.h"
31
32
#if !defined( LIBFSAPFS_HAVE_BFIO )
33
34
/* Opens a container using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBFSAPFS_EXTERN \
38
int libfsapfs_container_open_file_io_handle(
39
      libfsapfs_container_t *container,
40
      libbfio_handle_t *file_io_handle,
41
      int access_flags,
42
      libfsapfs_error_t **error );
43
44
#endif /* !defined( LIBFSAPFS_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
      const uint8_t *data,
48
      size_t size )
49
2.29k
{
50
2.29k
  uint8_t utf8_string[ 64 ];
51
2.29k
  uint16_t utf16_string[ 32 ];
52
53
2.29k
  libbfio_handle_t *file_io_handle = NULL;
54
2.29k
  libfsapfs_container_t *container = NULL;
55
2.29k
  libfsapfs_volume_t *volume       = NULL;
56
2.29k
  libfsapfs_snapshot_t *snapshot   = NULL;
57
2.29k
  size_t utf8_string_size          = 0;
58
2.29k
  size_t utf16_string_size         = 0;
59
2.29k
  int number_of_snapshots          = 0;
60
2.29k
  int number_of_volumes            = 0;
61
2.29k
  int snapshot_index               = 0;
62
2.29k
  int result                       = 0;
63
64
2.29k
  if( libbfio_memory_range_initialize(
65
2.29k
       &file_io_handle,
66
2.29k
       NULL ) != 1 )
67
0
  {
68
0
    return( 0 );
69
0
  }
70
2.29k
  if( libbfio_memory_range_set(
71
2.29k
       file_io_handle,
72
2.29k
       (uint8_t *) data,
73
2.29k
       size,
74
2.29k
       NULL ) != 1 )
75
0
  {
76
0
    goto on_error_libbfio;
77
0
  }
78
2.29k
  if( libfsapfs_container_initialize(
79
2.29k
       &container,
80
2.29k
       NULL ) != 1 )
81
0
  {
82
0
    goto on_error_libbfio;
83
0
  }
84
2.29k
  if( libfsapfs_container_open_file_io_handle(
85
2.29k
       container,
86
2.29k
       file_io_handle,
87
2.29k
       LIBFSAPFS_OPEN_READ,
88
2.29k
       NULL ) != 1 )
89
1.10k
  {
90
1.10k
    goto on_error_libfsapfs_container;
91
1.10k
  }
92
1.19k
  if( libfsapfs_container_get_number_of_volumes(
93
1.19k
       container,
94
1.19k
       &number_of_volumes,
95
1.19k
       NULL ) != 1 )
96
0
  {
97
0
    goto on_error_libfsapfs_container;
98
0
  }
99
1.19k
  if( number_of_volumes > 0 )
100
1.19k
  {
101
1.19k
    result = libfsapfs_container_get_volume_by_index(
102
1.19k
              container,
103
1.19k
              0,
104
1.19k
              &volume,
105
1.19k
              NULL );
106
107
1.19k
    if( result == -1 )
108
1.17k
    {
109
1.17k
      goto on_error_libfsapfs_container;
110
1.17k
    }
111
18
    if( result == 1 )
112
18
    {
113
18
      if( libfsapfs_volume_get_number_of_snapshots(
114
18
           volume,
115
18
           &number_of_snapshots,
116
18
           NULL ) != 1 )
117
0
      {
118
0
        goto on_error_libfsapfs_volume;
119
0
      }
120
18
      for( snapshot_index = 0;
121
18
           ( snapshot_index < number_of_snapshots )
122
1
         && ( snapshot_index < 16 );
123
18
           snapshot_index++ )
124
1
      {
125
1
        result = libfsapfs_volume_get_snapshot_by_index(
126
1
                  volume,
127
1
                  snapshot_index,
128
1
                  &snapshot,
129
1
                  NULL );
130
131
1
        if( result == -1 )
132
1
        {
133
1
          goto on_error_libfsapfs_snapshot;
134
1
        }
135
0
        if( result != 1 )
136
0
        {
137
0
          continue;
138
0
        }
139
0
        if( libfsapfs_snapshot_get_utf8_name_size(
140
0
             snapshot,
141
0
             &utf8_string_size,
142
0
             NULL ) == -1 )
143
0
        {
144
0
          goto on_error_libfsapfs_snapshot;
145
0
        }
146
0
        if( utf8_string_size > 0 )
147
0
        {
148
0
          if( libfsapfs_snapshot_get_utf8_name(
149
0
               snapshot,
150
0
               utf8_string,
151
0
               64,
152
0
               NULL ) == -1 )
153
0
          {
154
0
            goto on_error_libfsapfs_snapshot;
155
0
          }
156
0
        }
157
0
        if( libfsapfs_snapshot_get_utf16_name_size(
158
0
             snapshot,
159
0
             &utf16_string_size,
160
0
             NULL ) == -1 )
161
0
        {
162
0
          goto on_error_libfsapfs_snapshot;
163
0
        }
164
0
        if( utf16_string_size > 0 )
165
0
        {
166
0
          if( libfsapfs_snapshot_get_utf16_name(
167
0
               snapshot,
168
0
               utf16_string,
169
0
               32,
170
0
               NULL ) == -1 )
171
0
          {
172
0
            goto on_error_libfsapfs_snapshot;
173
0
          }
174
0
        }
175
0
        libfsapfs_snapshot_free(
176
0
         &snapshot,
177
0
         NULL );
178
0
      }
179
18
on_error_libfsapfs_snapshot:
180
18
      libfsapfs_snapshot_free(
181
18
       &snapshot,
182
18
       NULL );
183
18
    }
184
18
on_error_libfsapfs_volume:
185
18
    libfsapfs_volume_free(
186
18
     &volume,
187
18
     NULL );
188
18
  }
189
18
  libfsapfs_container_close(
190
18
   container,
191
18
   NULL );
192
193
2.29k
on_error_libfsapfs_container:
194
2.29k
  libfsapfs_container_free(
195
2.29k
   &container,
196
2.29k
   NULL );
197
198
2.29k
on_error_libbfio:
199
2.29k
  libbfio_handle_free(
200
2.29k
   &file_io_handle,
201
2.29k
   NULL );
202
203
2.29k
  return( 0 );
204
2.29k
}
205
206
} /* extern "C" */