/src/libewf/ossfuzz/handle_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * OSS-Fuzz target for libewf file type |
3 | | * |
4 | | * Copyright (C) 2006-2023, 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_libewf.h" |
31 | | |
32 | | #if !defined( LIBEWF_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a set of EWF file(s) using a Basic File IO (bfio) pool |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBEWF_EXTERN \ |
38 | | int libewf_handle_open_file_io_pool( |
39 | | libewf_handle_t *handle, |
40 | | libbfio_pool_t *file_io_pool, |
41 | | int access_flags, |
42 | | libewf_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBEWF_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 3.12k | { |
50 | 3.12k | libbfio_handle_t *file_io_handle = NULL; |
51 | 3.12k | libbfio_pool_t *file_io_pool = NULL; |
52 | 3.12k | libewf_handle_t *handle = NULL; |
53 | 3.12k | int entry_index = 0; |
54 | | |
55 | 3.12k | if( libbfio_memory_range_initialize( |
56 | 3.12k | &file_io_handle, |
57 | 3.12k | NULL ) != 1 ) |
58 | 0 | { |
59 | 0 | return( 0 ); |
60 | 0 | } |
61 | 3.12k | if( libbfio_memory_range_set( |
62 | 3.12k | file_io_handle, |
63 | 3.12k | (uint8_t *) data, |
64 | 3.12k | size, |
65 | 3.12k | NULL ) != 1 ) |
66 | 0 | { |
67 | 0 | goto on_error_libbfio; |
68 | 0 | } |
69 | 3.12k | if( libbfio_pool_initialize( |
70 | 3.12k | &file_io_pool, |
71 | 3.12k | 0, |
72 | 3.12k | 0, |
73 | 3.12k | NULL ) != 1 ) |
74 | 0 | { |
75 | 0 | goto on_error_libbfio; |
76 | 0 | } |
77 | 3.12k | if( libbfio_pool_append_handle( |
78 | 3.12k | file_io_pool, |
79 | 3.12k | &entry_index, |
80 | 3.12k | file_io_handle, |
81 | 3.12k | LIBBFIO_OPEN_READ, |
82 | 3.12k | NULL ) != 1 ) |
83 | 0 | { |
84 | 0 | goto on_error_libbfio; |
85 | 0 | } |
86 | | /* The file IO pool takes over management of the file IO handle |
87 | | */ |
88 | 3.12k | file_io_handle = NULL; |
89 | | |
90 | 3.12k | if( libewf_handle_initialize( |
91 | 3.12k | &handle, |
92 | 3.12k | NULL ) != 1 ) |
93 | 0 | { |
94 | 0 | goto on_error_libbfio; |
95 | 0 | } |
96 | 3.12k | if( libewf_handle_open_file_io_pool( |
97 | 3.12k | handle, |
98 | 3.12k | file_io_pool, |
99 | 3.12k | LIBEWF_OPEN_READ, |
100 | 3.12k | NULL ) != 1 ) |
101 | 1.96k | { |
102 | 1.96k | goto on_error_libewf; |
103 | 1.96k | } |
104 | 1.16k | libewf_handle_close( |
105 | 1.16k | handle, |
106 | 1.16k | NULL ); |
107 | | |
108 | 3.12k | on_error_libewf: |
109 | 3.12k | libewf_handle_free( |
110 | 3.12k | &handle, |
111 | 3.12k | NULL ); |
112 | | |
113 | 3.12k | on_error_libbfio: |
114 | | /* Note that on error the handle still has a reference to file_io_pool |
115 | | * that will be closed. Therefore the file IO pool and handle need to |
116 | | * be freed after closing or freeing the handle. |
117 | | */ |
118 | 3.12k | if( file_io_pool != NULL ) |
119 | 3.12k | { |
120 | 3.12k | libbfio_pool_free( |
121 | 3.12k | &file_io_pool, |
122 | 3.12k | NULL ); |
123 | 3.12k | } |
124 | 3.12k | if( file_io_handle != NULL ) |
125 | 0 | { |
126 | 0 | libbfio_handle_free( |
127 | 0 | &file_io_handle, |
128 | 0 | NULL ); |
129 | 0 | } |
130 | 3.12k | return( 0 ); |
131 | 3.12k | } |
132 | | |
133 | | } /* extern "C" */ |
134 | | |