/src/libvsapm/ossfuzz/partition_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * OSS-Fuzz target for libvsapm partition type |
3 | | * |
4 | | * Copyright (C) 2009-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_libvsapm.h" |
31 | | |
32 | | #if !defined( LIBVSAPM_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a volume using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBVSAPM_EXTERN \ |
38 | | int libvsapm_volume_open_file_io_handle( |
39 | | libvsapm_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libvsapm_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBVSAPM_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 340 | { |
50 | 340 | libbfio_handle_t *file_io_handle = NULL; |
51 | 340 | libvsapm_partition_t *partition = NULL; |
52 | 340 | libvsapm_volume_t *volume = NULL; |
53 | 340 | int number_of_partitions = 0; |
54 | | |
55 | 340 | if( libbfio_memory_range_initialize( |
56 | 340 | &file_io_handle, |
57 | 340 | NULL ) != 1 ) |
58 | 0 | { |
59 | 0 | return( 0 ); |
60 | 0 | } |
61 | 340 | if( libbfio_memory_range_set( |
62 | 340 | file_io_handle, |
63 | 340 | (uint8_t *) data, |
64 | 340 | size, |
65 | 340 | NULL ) != 1 ) |
66 | 0 | { |
67 | 0 | goto on_error_libbfio; |
68 | 0 | } |
69 | 340 | if( libvsapm_volume_initialize( |
70 | 340 | &volume, |
71 | 340 | NULL ) != 1 ) |
72 | 0 | { |
73 | 0 | goto on_error_libbfio; |
74 | 0 | } |
75 | 340 | if( libvsapm_volume_open_file_io_handle( |
76 | 340 | volume, |
77 | 340 | file_io_handle, |
78 | 340 | LIBVSAPM_OPEN_READ, |
79 | 340 | NULL ) != 1 ) |
80 | 295 | { |
81 | 295 | goto on_error_libvsapm_volume; |
82 | 295 | } |
83 | 45 | if( libvsapm_volume_get_number_of_partitions( |
84 | 45 | volume, |
85 | 45 | &number_of_partitions, |
86 | 45 | NULL ) != 1 ) |
87 | 0 | { |
88 | 0 | goto on_error_libvsapm_volume; |
89 | 0 | } |
90 | 45 | if( number_of_partitions > 0 ) |
91 | 44 | { |
92 | 44 | if( libvsapm_volume_get_partition_by_index( |
93 | 44 | volume, |
94 | 44 | 0, |
95 | 44 | &partition, |
96 | 44 | NULL ) == 1 ) |
97 | 44 | { |
98 | 44 | libvsapm_partition_free( |
99 | 44 | &partition, |
100 | 44 | NULL ); |
101 | 44 | } |
102 | 44 | } |
103 | 45 | libvsapm_volume_close( |
104 | 45 | volume, |
105 | 45 | NULL ); |
106 | | |
107 | 340 | on_error_libvsapm_volume: |
108 | 340 | libvsapm_volume_free( |
109 | 340 | &volume, |
110 | 340 | NULL ); |
111 | | |
112 | 340 | on_error_libbfio: |
113 | 340 | libbfio_handle_free( |
114 | 340 | &file_io_handle, |
115 | 340 | NULL ); |
116 | | |
117 | 340 | return( 0 ); |
118 | 340 | } |
119 | | |
120 | | } /* extern "C" */ |
121 | | |