Debian Codegraph @

ossfuzz/file_fuzzer.cc

raw
1 /*
2 * OSS-Fuzz target for libvhdi file type
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
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_libvhdi.h"
31
32 #if !defined( LIBVHDI_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 LIBVHDI_EXTERN \
38 int libvhdi_file_open_file_io_handle(
39 libvhdi_file_t *file,
40 libbfio_handle_t *file_io_handle,
41 int access_flags,
42 libvhdi_error_t **error );
43
44 #endif /* !defined( LIBVHDI_HAVE_BFIO ) */
45
46 int LLVMFuzzerTestOneInput(
47 const uint8_t *data,
48 size_t size )
49 {
50 libbfio_handle_t *file_io_handle = NULL;
51 libvhdi_file_t *file = NULL;
52
53 if( libbfio_memory_range_initialize(
54 &file_io_handle,
55 NULL ) != 1 )
56 {
57 return( 0 );
58 }
59 if( libbfio_memory_range_set(
60 file_io_handle,
61 (uint8_t *) data,
62 size,
63 NULL ) != 1 )
64 {
65 goto on_error_libbfio;
66 }
67 if( libvhdi_file_initialize(
68 &file,
69 NULL ) != 1 )
70 {
71 goto on_error_libbfio;
72 }
73 if( libvhdi_file_open_file_io_handle(
74 file,
75 file_io_handle,
76 LIBVHDI_OPEN_READ,
77 NULL ) != 1 )
78 {
79 goto on_error_libvhdi;
80 }
81 libvhdi_file_close(
82 file,
83 NULL );
84
85 on_error_libvhdi:
86 libvhdi_file_free(
87 &file,
88 NULL );
89
90 on_error_libbfio:
91 libbfio_handle_free(
92 &file_io_handle,
93 NULL );
94
95 return( 0 );
96 }
97
98 } /* extern "C" */
99