/src/libwtcdb/ossfuzz/cache_entry_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libwtcdb cache_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_cache_entry.h" |
33 | | |
34 | | /* The cache entry format versions for which distinct header formats exist |
35 | | */ |
36 | | static const uint32_t format_versions[ 3 ] = { |
37 | | 20, |
38 | | 21, |
39 | | 30 |
40 | | }; |
41 | | |
42 | | int LLVMFuzzerTestOneInput( |
43 | | const uint8_t *data, |
44 | | size_t size ) |
45 | 364 | { |
46 | 364 | libbfio_handle_t *file_io_handle = NULL; |
47 | 364 | libwtcdb_io_handle_t *io_handle = NULL; |
48 | 364 | libwtcdb_cache_entry_t *cache_entry = NULL; |
49 | 364 | size_t version_index = 0; |
50 | | |
51 | 364 | if( libbfio_memory_range_initialize( |
52 | 364 | &file_io_handle, |
53 | 364 | NULL ) != 1 ) |
54 | 0 | { |
55 | 0 | return( 0 ); |
56 | 0 | } |
57 | 364 | if( libbfio_memory_range_set( |
58 | 364 | file_io_handle, |
59 | 364 | (uint8_t *) data, |
60 | 364 | size, |
61 | 364 | NULL ) != 1 ) |
62 | 0 | { |
63 | 0 | goto on_error_libbfio; |
64 | 0 | } |
65 | 364 | if( libbfio_handle_open( |
66 | 364 | file_io_handle, |
67 | 364 | LIBBFIO_OPEN_READ, |
68 | 364 | NULL ) != 1 ) |
69 | 0 | { |
70 | 0 | goto on_error_libbfio; |
71 | 0 | } |
72 | 364 | if( libwtcdb_io_handle_initialize( |
73 | 364 | &io_handle, |
74 | 364 | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | goto on_error_libbfio; |
77 | 0 | } |
78 | 364 | if( libwtcdb_cache_entry_initialize( |
79 | 364 | &cache_entry, |
80 | 364 | NULL ) != 1 ) |
81 | 0 | { |
82 | 0 | goto on_error_libwtcdb_io_handle; |
83 | 0 | } |
84 | 364 | for( version_index = 0; |
85 | 1.45k | version_index < ( sizeof( format_versions ) / sizeof( format_versions[ 0 ] ) ); |
86 | 1.09k | version_index++ ) |
87 | 1.09k | { |
88 | 1.09k | io_handle->file_type = LIBWTCDB_FILE_TYPE_CACHE; |
89 | 1.09k | io_handle->format_version = format_versions[ version_index ]; |
90 | | |
91 | 1.09k | libwtcdb_cache_entry_header_read_data( |
92 | 1.09k | cache_entry, |
93 | 1.09k | io_handle, |
94 | 1.09k | data, |
95 | 1.09k | size, |
96 | 1.09k | NULL ); |
97 | 1.09k | libwtcdb_cache_entry_read_file_io_handle( |
98 | 1.09k | cache_entry, |
99 | 1.09k | io_handle, |
100 | 1.09k | file_io_handle, |
101 | 1.09k | 0, |
102 | 1.09k | NULL ); |
103 | 1.09k | } |
104 | | |
105 | 364 | on_error_libwtcdb_cache_entry: |
106 | 364 | libwtcdb_cache_entry_free( |
107 | 364 | &cache_entry, |
108 | 364 | NULL ); |
109 | | |
110 | 364 | on_error_libwtcdb_io_handle: |
111 | 364 | libwtcdb_io_handle_free( |
112 | 364 | &io_handle, |
113 | 364 | NULL ); |
114 | | |
115 | 364 | on_error_libbfio: |
116 | 364 | libbfio_handle_free( |
117 | 364 | &file_io_handle, |
118 | 364 | NULL ); |
119 | | |
120 | 364 | return( 0 ); |
121 | 364 | } |
122 | | |
123 | | } /* extern "C" */ |