/src/libesedb/ossfuzz/table_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libesedb table type |
3 | | * |
4 | | * Copyright (C) 2011-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_libesedb.h" |
31 | | |
32 | | #if !defined( LIBESEDB_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 | | LIBESEDB_EXTERN \ |
38 | | int libesedb_file_open_file_io_handle( |
39 | | libesedb_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libesedb_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBESEDB_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 1.72k | { |
50 | 1.72k | uint8_t utf8_name[ 64 ]; |
51 | | |
52 | 1.72k | libbfio_handle_t *file_io_handle = NULL; |
53 | 1.72k | libesedb_file_t *file = NULL; |
54 | 1.72k | libesedb_table_t *table = NULL; |
55 | 1.72k | size_t utf8_name_size = 0; |
56 | 1.72k | int number_of_columns = 0; |
57 | 1.72k | int number_of_indexes = 0; |
58 | 1.72k | int number_of_records = 0; |
59 | 1.72k | int number_of_tables = 0; |
60 | | |
61 | 1.72k | if( libbfio_memory_range_initialize( |
62 | 1.72k | &file_io_handle, |
63 | 1.72k | NULL ) != 1 ) |
64 | 0 | { |
65 | 0 | return( 0 ); |
66 | 0 | } |
67 | 1.72k | if( libbfio_memory_range_set( |
68 | 1.72k | file_io_handle, |
69 | 1.72k | (uint8_t *) data, |
70 | 1.72k | size, |
71 | 1.72k | NULL ) != 1 ) |
72 | 0 | { |
73 | 0 | goto on_error_libbfio; |
74 | 0 | } |
75 | 1.72k | if( libesedb_file_initialize( |
76 | 1.72k | &file, |
77 | 1.72k | NULL ) != 1 ) |
78 | 0 | { |
79 | 0 | goto on_error_libbfio; |
80 | 0 | } |
81 | 1.72k | if( libesedb_file_open_file_io_handle( |
82 | 1.72k | file, |
83 | 1.72k | file_io_handle, |
84 | 1.72k | LIBESEDB_OPEN_READ, |
85 | 1.72k | NULL ) != 1 ) |
86 | 1.34k | { |
87 | 1.34k | goto on_error_libesedb_file; |
88 | 1.34k | } |
89 | 382 | if( libesedb_file_get_number_of_tables( |
90 | 382 | file, |
91 | 382 | &number_of_tables, |
92 | 382 | NULL ) != 1 ) |
93 | 0 | { |
94 | 0 | goto on_error_libesedb_file; |
95 | 0 | } |
96 | 382 | if( number_of_tables > 0 ) |
97 | 320 | { |
98 | 320 | if( libesedb_file_get_table( |
99 | 320 | file, |
100 | 320 | 0, |
101 | 320 | &table, |
102 | 320 | NULL ) != 1 ) |
103 | 162 | { |
104 | 162 | goto on_error_libesedb_file; |
105 | 162 | } |
106 | 158 | if( libesedb_table_get_utf8_name_size( |
107 | 158 | table, |
108 | 158 | &utf8_name_size, |
109 | 158 | NULL ) != 1 ) |
110 | 5 | { |
111 | 5 | goto on_error_libesedb_table; |
112 | 5 | } |
113 | 153 | if( utf8_name_size > 0 ) |
114 | 153 | { |
115 | 153 | if( libesedb_table_get_utf8_name( |
116 | 153 | table, |
117 | 153 | utf8_name, |
118 | 153 | 64, |
119 | 153 | NULL ) != 1 ) |
120 | 12 | { |
121 | 12 | goto on_error_libesedb_table; |
122 | 12 | } |
123 | 153 | } |
124 | 141 | if( libesedb_table_get_number_of_columns( |
125 | 141 | table, |
126 | 141 | &number_of_columns, |
127 | 141 | 0, |
128 | 141 | NULL ) != 1 ) |
129 | 0 | { |
130 | 0 | goto on_error_libesedb_table; |
131 | 0 | } |
132 | 141 | if( libesedb_table_get_number_of_records( |
133 | 141 | table, |
134 | 141 | &number_of_records, |
135 | 141 | NULL ) != 1 ) |
136 | 119 | { |
137 | 119 | goto on_error_libesedb_table; |
138 | 119 | } |
139 | 22 | if( libesedb_table_get_number_of_indexes( |
140 | 22 | table, |
141 | 22 | &number_of_indexes, |
142 | 22 | NULL ) != 1 ) |
143 | 0 | { |
144 | 0 | goto on_error_libesedb_table; |
145 | 0 | } |
146 | 158 | on_error_libesedb_table: |
147 | 158 | libesedb_table_free( |
148 | 158 | &table, |
149 | 158 | NULL ); |
150 | 158 | } |
151 | 220 | libesedb_file_close( |
152 | 220 | file, |
153 | 220 | NULL ); |
154 | | |
155 | 1.72k | on_error_libesedb_file: |
156 | 1.72k | libesedb_file_free( |
157 | 1.72k | &file, |
158 | 1.72k | NULL ); |
159 | | |
160 | 1.72k | on_error_libbfio: |
161 | 1.72k | libbfio_handle_free( |
162 | 1.72k | &file_io_handle, |
163 | 1.72k | NULL ); |
164 | | |
165 | 1.72k | return( 0 ); |
166 | 1.72k | } |
167 | | |
168 | | } /* extern "C" */ |