/src/libwtcdb/ossfuzz/index_entry_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libwtcdb index_entry 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 | | #include "../libwtcdb/libwtcdb_index_entry.h" |
33 | | |
34 | | /* The index entry format versions |
35 | | */ |
36 | | static const uint32_t format_versions[ 5 ] = { |
37 | | 20, |
38 | | 21, |
39 | | 30, |
40 | | 31, |
41 | | 32 |
42 | | }; |
43 | | |
44 | | int LLVMFuzzerTestOneInput( |
45 | | const uint8_t *data, |
46 | | size_t size ) |
47 | 70 | { |
48 | 70 | libbfio_handle_t *file_io_handle = NULL; |
49 | 70 | libwtcdb_io_handle_t *io_handle = NULL; |
50 | 70 | libwtcdb_index_entry_t *index_entry = NULL; |
51 | 70 | size_t version_index = 0; |
52 | | |
53 | 70 | if( libbfio_memory_range_initialize( |
54 | 70 | &file_io_handle, |
55 | 70 | NULL ) != 1 ) |
56 | 0 | { |
57 | 0 | return( 0 ); |
58 | 0 | } |
59 | 70 | if( libbfio_memory_range_set( |
60 | 70 | file_io_handle, |
61 | 70 | (uint8_t *) data, |
62 | 70 | size, |
63 | 70 | NULL ) != 1 ) |
64 | 0 | { |
65 | 0 | goto on_error_libbfio; |
66 | 0 | } |
67 | 70 | if( libbfio_handle_open( |
68 | 70 | file_io_handle, |
69 | 70 | LIBBFIO_OPEN_READ, |
70 | 70 | NULL ) != 1 ) |
71 | 0 | { |
72 | 0 | goto on_error_libbfio; |
73 | 0 | } |
74 | 70 | if( libwtcdb_io_handle_initialize( |
75 | 70 | &io_handle, |
76 | 70 | NULL ) != 1 ) |
77 | 0 | { |
78 | 0 | goto on_error_libbfio; |
79 | 0 | } |
80 | 70 | if( libwtcdb_index_entry_initialize( |
81 | 70 | &index_entry, |
82 | 70 | NULL ) != 1 ) |
83 | 0 | { |
84 | 0 | goto on_error_libwtcdb_io_handle; |
85 | 0 | } |
86 | 70 | for( version_index = 0; |
87 | 420 | version_index < ( sizeof( format_versions ) / sizeof( format_versions[ 0 ] ) ); |
88 | 350 | version_index++ ) |
89 | 350 | { |
90 | 350 | if( format_versions[ version_index ] < 30 ) |
91 | 140 | { |
92 | 140 | io_handle->file_type = LIBWTCDB_FILE_TYPE_INDEX_V20; |
93 | 140 | } |
94 | 210 | else |
95 | 210 | { |
96 | 210 | io_handle->file_type = LIBWTCDB_FILE_TYPE_INDEX_V30; |
97 | 210 | } |
98 | 350 | io_handle->format_version = format_versions[ version_index ]; |
99 | | |
100 | 350 | libwtcdb_index_entry_read_data( |
101 | 350 | index_entry, |
102 | 350 | io_handle, |
103 | 350 | data, |
104 | 350 | size, |
105 | 350 | NULL ); |
106 | 350 | libwtcdb_index_entry_read_file_io_handle( |
107 | 350 | index_entry, |
108 | 350 | io_handle, |
109 | 350 | file_io_handle, |
110 | 350 | 0, |
111 | 350 | NULL ); |
112 | 350 | } |
113 | | |
114 | 70 | on_error_libwtcdb_index_entry: |
115 | 70 | libwtcdb_index_entry_free( |
116 | 70 | &index_entry, |
117 | 70 | NULL ); |
118 | | |
119 | 70 | on_error_libwtcdb_io_handle: |
120 | 70 | libwtcdb_io_handle_free( |
121 | 70 | &io_handle, |
122 | 70 | NULL ); |
123 | | |
124 | 70 | on_error_libbfio: |
125 | 70 | libbfio_handle_free( |
126 | 70 | &file_io_handle, |
127 | 70 | NULL ); |
128 | | |
129 | 70 | return( 0 ); |
130 | 70 | } |
131 | | |
132 | | } /* extern "C" */ |