/src/libwtcdb/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libwtcdb 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_libwtcdb.h" |
31 | | |
32 | | #if !defined( LIBWTCDB_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 | | LIBWTCDB_EXTERN \ |
38 | | int libwtcdb_file_open_file_io_handle( |
39 | | libwtcdb_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libwtcdb_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBWTCDB_HAVE_BFIO ) */ |
45 | | |
46 | | /* Limits on the number of items to query |
47 | | */ |
48 | 1.25k | #define FUZZER_ITEMS_LIMIT 8 |
49 | | |
50 | | int LLVMFuzzerTestOneInput( |
51 | | const uint8_t *data, |
52 | | size_t size ) |
53 | 973 | { |
54 | 973 | libbfio_handle_t *file_io_handle = NULL; |
55 | 973 | libwtcdb_file_t *file = NULL; |
56 | 973 | libwtcdb_item_t *item = NULL; |
57 | 973 | uint8_t file_type = 0; |
58 | 973 | int item_index = 0; |
59 | 973 | int number_of_items = 0; |
60 | | |
61 | 973 | if( libbfio_memory_range_initialize( |
62 | 973 | &file_io_handle, |
63 | 973 | NULL ) != 1 ) |
64 | 0 | { |
65 | 0 | return( 0 ); |
66 | 0 | } |
67 | 973 | if( libbfio_memory_range_set( |
68 | 973 | file_io_handle, |
69 | 973 | (uint8_t *) data, |
70 | 973 | size, |
71 | 973 | NULL ) != 1 ) |
72 | 0 | { |
73 | 0 | goto on_error_libbfio; |
74 | 0 | } |
75 | 973 | if( libwtcdb_file_initialize( |
76 | 973 | &file, |
77 | 973 | NULL ) != 1 ) |
78 | 0 | { |
79 | 0 | goto on_error_libbfio; |
80 | 0 | } |
81 | 973 | if( libwtcdb_file_open_file_io_handle( |
82 | 973 | file, |
83 | 973 | file_io_handle, |
84 | 973 | LIBWTCDB_OPEN_READ, |
85 | 973 | NULL ) != 1 ) |
86 | 564 | { |
87 | 564 | goto on_error_libwtcdb_file; |
88 | 564 | } |
89 | 409 | libwtcdb_file_get_type( |
90 | 409 | file, |
91 | 409 | &file_type, |
92 | 409 | NULL ); |
93 | 409 | libwtcdb_file_signal_abort( |
94 | 409 | file, |
95 | 409 | NULL ); |
96 | 409 | if( libwtcdb_file_get_number_of_items( |
97 | 409 | file, |
98 | 409 | &number_of_items, |
99 | 409 | NULL ) == 1 ) |
100 | 409 | { |
101 | 409 | for( item_index = 0; |
102 | 1.56k | item_index < number_of_items; |
103 | 1.16k | item_index++ ) |
104 | 1.25k | { |
105 | 1.25k | if( item_index >= FUZZER_ITEMS_LIMIT ) |
106 | 98 | { |
107 | 98 | break; |
108 | 98 | } |
109 | 1.16k | item = NULL; |
110 | 1.16k | libwtcdb_file_get_item( |
111 | 1.16k | file, |
112 | 1.16k | item_index, |
113 | 1.16k | &item, |
114 | 1.16k | NULL ); |
115 | 1.16k | if( item != NULL ) |
116 | 1.16k | { |
117 | 1.16k | libwtcdb_item_free( |
118 | 1.16k | &item, |
119 | 1.16k | NULL ); |
120 | 1.16k | } |
121 | 1.16k | } |
122 | | /* Attempt to retrieve an item using an out of range index |
123 | | * to trigger the validation |
124 | | */ |
125 | 409 | item = NULL; |
126 | 409 | libwtcdb_file_get_item( |
127 | 409 | file, |
128 | 409 | number_of_items, |
129 | 409 | &item, |
130 | 409 | NULL ); |
131 | 409 | if( item != NULL ) |
132 | 0 | { |
133 | 0 | libwtcdb_item_free( |
134 | 0 | &item, |
135 | 0 | NULL ); |
136 | 0 | } |
137 | 409 | } |
138 | 409 | libwtcdb_file_close( |
139 | 409 | file, |
140 | 409 | NULL ); |
141 | | /* Attempt to open with unsupported access flags |
142 | | * to trigger the validation |
143 | | */ |
144 | 409 | libwtcdb_file_open_file_io_handle( |
145 | 409 | file, |
146 | 409 | file_io_handle, |
147 | 409 | LIBWTCDB_OPEN_READ_WRITE, |
148 | 409 | NULL ); |
149 | 409 | libwtcdb_file_open_file_io_handle( |
150 | 409 | file, |
151 | 409 | file_io_handle, |
152 | 409 | 0, |
153 | 409 | NULL ); |
154 | 409 | libwtcdb_file_close( |
155 | 409 | file, |
156 | 409 | NULL ); |
157 | | |
158 | 973 | on_error_libwtcdb_file: |
159 | 973 | libwtcdb_file_free( |
160 | 973 | &file, |
161 | 973 | NULL ); |
162 | | |
163 | 973 | on_error_libbfio: |
164 | 973 | libbfio_handle_free( |
165 | 973 | &file_io_handle, |
166 | 973 | NULL ); |
167 | | |
168 | 973 | return( 0 ); |
169 | 973 | } |
170 | | |
171 | | } /* extern "C" */ |