Coverage Report

Created: 2025-06-24 07:14

/src/libfsapfs/ossfuzz/extended_attribute_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * OSS-Fuzz target for libfsapfs extended_attribute type
3
 *
4
 * Copyright (C) 2018-2024, 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
4.57k
{
50
4.57k
  libbfio_handle_t *file_io_handle                   = NULL;
51
4.57k
  libfsapfs_container_t *container                   = NULL;
52
4.57k
  libfsapfs_extended_attribute_t *extended_attribute = NULL;
53
4.57k
  libfsapfs_file_entry_t *file_entry                 = NULL;
54
4.57k
  libfsapfs_volume_t *volume                         = NULL;
55
4.57k
  int number_of_extended_attributes                  = 0;
56
4.57k
  int number_of_volumes                              = 0;
57
4.57k
  int result                                         = 0;
58
59
4.57k
  if( libbfio_memory_range_initialize(
60
4.57k
       &file_io_handle,
61
4.57k
       NULL ) != 1 )
62
0
  {
63
0
    return( 0 );
64
0
  }
65
4.57k
  if( libbfio_memory_range_set(
66
4.57k
       file_io_handle,
67
4.57k
       (uint8_t *) data,
68
4.57k
       size,
69
4.57k
       NULL ) != 1 )
70
0
  {
71
0
    goto on_error_libbfio;
72
0
  }
73
4.57k
  if( libfsapfs_container_initialize(
74
4.57k
       &container,
75
4.57k
       NULL ) != 1 )
76
0
  {
77
0
    goto on_error_libbfio;
78
0
  }
79
4.57k
  if( libfsapfs_container_open_file_io_handle(
80
4.57k
       container,
81
4.57k
       file_io_handle,
82
4.57k
       LIBFSAPFS_OPEN_READ,
83
4.57k
       NULL ) != 1 )
84
1.37k
  {
85
1.37k
    goto on_error_libfsapfs_container;
86
1.37k
  }
87
3.19k
  if( libfsapfs_container_get_number_of_volumes(
88
3.19k
       container,
89
3.19k
       &number_of_volumes,
90
3.19k
       NULL ) != 1 )
91
0
  {
92
0
    goto on_error_libfsapfs_container;
93
0
  }
94
3.19k
  if( number_of_volumes > 0 )
95
3.19k
  {
96
3.19k
    if( libfsapfs_container_get_volume_by_index(
97
3.19k
         container,
98
3.19k
         0,
99
3.19k
         &volume,
100
3.19k
         NULL ) != 1 )
101
1.87k
    {
102
1.87k
      goto on_error_libfsapfs_container;
103
1.87k
    }
104
1.32k
    result = libfsapfs_volume_get_file_entry_by_utf8_path(
105
1.32k
              volume,
106
1.32k
              (uint8_t *) "/a_directory/a_file",
107
1.32k
              19,
108
1.32k
              &file_entry,
109
1.32k
              NULL );
110
111
1.32k
    if( result == 1 )
112
260
    {
113
260
      if( libfsapfs_file_entry_get_number_of_extended_attributes(
114
260
           file_entry,
115
260
           &number_of_extended_attributes,
116
260
           NULL ) != 1 )
117
111
      {
118
111
        goto on_error_libfsapfs_file_entry;
119
111
      }
120
149
      if( number_of_extended_attributes > 0 )
121
66
      {
122
66
        if( libfsapfs_file_entry_get_extended_attribute_by_index(
123
66
             file_entry,
124
66
             0,
125
66
             &extended_attribute,
126
66
             NULL ) != 1 )
127
0
        {
128
0
          goto on_error_libfsapfs_file_entry;
129
0
        }
130
66
        libfsapfs_extended_attribute_free(
131
66
         &extended_attribute,
132
66
         NULL );
133
66
      }
134
260
on_error_libfsapfs_file_entry:
135
260
      libfsapfs_file_entry_free(
136
260
       &file_entry,
137
260
       NULL );
138
260
    }
139
1.32k
    libfsapfs_volume_free(
140
1.32k
     &volume,
141
1.32k
     NULL );
142
1.32k
  }
143
4.57k
on_error_libfsapfs_container:
144
4.57k
  libfsapfs_container_free(
145
4.57k
   &container,
146
4.57k
   NULL );
147
148
4.57k
on_error_libbfio:
149
4.57k
  libbfio_handle_free(
150
4.57k
   &file_io_handle,
151
4.57k
   NULL );
152
153
4.57k
  return( 0 );
154
4.57k
}
155
156
} /* extern "C" */
157