/src/libscca/ossfuzz/file_metrics_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libscca file metrics 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_libscca.h" |
31 | | |
32 | | #if !defined( LIBSCCA_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 | | LIBSCCA_EXTERN \ |
38 | | int libscca_file_open_file_io_handle( |
39 | | libscca_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libscca_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBSCCA_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 2.32k | { |
50 | 2.32k | uint8_t utf8_string[ 512 ]; |
51 | 2.32k | uint16_t utf16_string[ 256 ]; |
52 | | |
53 | 2.32k | libbfio_handle_t *file_io_handle = NULL; |
54 | 2.32k | libscca_file_t *file = NULL; |
55 | 2.32k | libscca_file_metrics_t *file_metrics = NULL; |
56 | 2.32k | size_t utf8_string_size = 0; |
57 | 2.32k | size_t utf16_string_size = 0; |
58 | 2.32k | uint64_t file_reference = 0; |
59 | 2.32k | int number_of_entries = 0; |
60 | | |
61 | 2.32k | if( libbfio_memory_range_initialize( |
62 | 2.32k | &file_io_handle, |
63 | 2.32k | NULL ) != 1 ) |
64 | 0 | { |
65 | 0 | return( 0 ); |
66 | 0 | } |
67 | 2.32k | if( libbfio_memory_range_set( |
68 | 2.32k | file_io_handle, |
69 | 2.32k | (uint8_t *) data, |
70 | 2.32k | size, |
71 | 2.32k | NULL ) != 1 ) |
72 | 0 | { |
73 | 0 | goto on_error_libbfio; |
74 | 0 | } |
75 | 2.32k | if( libscca_file_initialize( |
76 | 2.32k | &file, |
77 | 2.32k | NULL ) != 1 ) |
78 | 0 | { |
79 | 0 | goto on_error_libbfio; |
80 | 0 | } |
81 | 2.32k | if( libscca_file_open_file_io_handle( |
82 | 2.32k | file, |
83 | 2.32k | file_io_handle, |
84 | 2.32k | LIBSCCA_OPEN_READ, |
85 | 2.32k | NULL ) != 1 ) |
86 | 1.98k | { |
87 | 1.98k | goto on_error_libscca; |
88 | 1.98k | } |
89 | 345 | if( libscca_file_get_number_of_file_metrics_entries( |
90 | 345 | file, |
91 | 345 | &number_of_entries, |
92 | 345 | NULL ) != 1 ) |
93 | 0 | { |
94 | 0 | goto on_error_libscca; |
95 | 0 | } |
96 | 345 | if( number_of_entries > 0 ) |
97 | 332 | { |
98 | 332 | if( libscca_file_get_file_metrics_entry( |
99 | 332 | file, |
100 | 332 | 0, |
101 | 332 | &file_metrics, |
102 | 332 | NULL ) != 1 ) |
103 | 0 | { |
104 | 0 | goto on_error_libscca; |
105 | 0 | } |
106 | 332 | if( libscca_file_metrics_get_utf8_filename_size( |
107 | 332 | file_metrics, |
108 | 332 | &utf8_string_size, |
109 | 332 | NULL ) != 1 ) |
110 | 68 | { |
111 | 68 | goto on_error_libscca_file_metrics; |
112 | 68 | } |
113 | 264 | if( libscca_file_metrics_get_utf8_filename( |
114 | 264 | file_metrics, |
115 | 264 | utf8_string, |
116 | 264 | 512, |
117 | 264 | NULL ) != 1 ) |
118 | 40 | { |
119 | 40 | goto on_error_libscca_file_metrics; |
120 | 40 | } |
121 | 224 | if( libscca_file_metrics_get_utf16_filename_size( |
122 | 224 | file_metrics, |
123 | 224 | &utf16_string_size, |
124 | 224 | NULL ) != 1 ) |
125 | 0 | { |
126 | 0 | goto on_error_libscca_file_metrics; |
127 | 0 | } |
128 | 224 | if( libscca_file_metrics_get_utf16_filename( |
129 | 224 | file_metrics, |
130 | 224 | utf16_string, |
131 | 224 | 256, |
132 | 224 | NULL ) != 1 ) |
133 | 2 | { |
134 | 2 | goto on_error_libscca_file_metrics; |
135 | 2 | } |
136 | 222 | if( libscca_file_metrics_get_file_reference( |
137 | 222 | file_metrics, |
138 | 222 | &file_reference, |
139 | 222 | NULL ) == -1 ) |
140 | 0 | { |
141 | 0 | goto on_error_libscca_file_metrics; |
142 | 0 | } |
143 | 332 | on_error_libscca_file_metrics: |
144 | 332 | libscca_file_metrics_free( |
145 | 332 | &file_metrics, |
146 | 332 | NULL ); |
147 | 332 | } |
148 | 345 | libscca_file_close( |
149 | 345 | file, |
150 | 345 | NULL ); |
151 | | |
152 | 2.32k | on_error_libscca: |
153 | 2.32k | libscca_file_free( |
154 | 2.32k | &file, |
155 | 2.32k | NULL ); |
156 | | |
157 | 2.32k | on_error_libbfio: |
158 | 2.32k | libbfio_handle_free( |
159 | 2.32k | &file_io_handle, |
160 | 2.32k | NULL ); |
161 | | |
162 | 2.32k | return( 0 ); |
163 | 2.32k | } |
164 | | |
165 | | } /* extern "C" */ |