/src/libsmraw/ossfuzz/handle_fuzzer.cc
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * OSS-Fuzz target for libsmraw file type  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2010-2024, 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_libsmraw.h"  | 
31  |  |  | 
32  |  | #if !defined( LIBSMRAW_HAVE_BFIO )  | 
33  |  |  | 
34  |  | /* Opens a set of storage media RAW files using a Basic File IO (bfio) pool  | 
35  |  |  * Returns 1 if successful or -1 on error  | 
36  |  |  */  | 
37  |  | LIBSMRAW_EXTERN \  | 
38  |  | int libsmraw_handle_open_file_io_pool(  | 
39  |  |      libsmraw_handle_t *handle,  | 
40  |  |      libbfio_handle_t *file_io_pool,  | 
41  |  |      int access_flags,  | 
42  |  |      libsmraw_error_t **error );  | 
43  |  |  | 
44  |  | #endif /* !defined( LIBSMRAW_HAVE_BFIO ) */  | 
45  |  |  | 
46  |  | int LLVMFuzzerTestOneInput(  | 
47  |  |      const uint8_t *data,  | 
48  |  |      size_t size )  | 
49  | 22  | { | 
50  | 22  |   libbfio_handle_t *file_io_handle = NULL;  | 
51  | 22  |   libbfio_pool_t *file_io_pool     = NULL;  | 
52  | 22  |   libsmraw_handle_t *handle        = NULL;  | 
53  | 22  |   int entry_index                  = 0;  | 
54  |  |  | 
55  | 22  |   if( libbfio_memory_range_initialize(  | 
56  | 22  |        &file_io_handle,  | 
57  | 22  |        NULL ) != 1 )  | 
58  | 0  |   { | 
59  | 0  |     return( 0 );  | 
60  | 0  |   }  | 
61  | 22  |   if( libbfio_memory_range_set(  | 
62  | 22  |        file_io_handle,  | 
63  | 22  |        (uint8_t *) data,  | 
64  | 22  |        size,  | 
65  | 22  |        NULL ) != 1 )  | 
66  | 0  |   { | 
67  | 0  |     goto on_error_libbfio;  | 
68  | 0  |   }  | 
69  | 22  |   if( libbfio_pool_initialize(  | 
70  | 22  |        &file_io_pool,  | 
71  | 22  |        0,  | 
72  | 22  |        0,  | 
73  | 22  |        NULL ) != 1 )  | 
74  | 0  |   { | 
75  | 0  |     goto on_error_libbfio;  | 
76  | 0  |   }  | 
77  | 22  |   if( libbfio_pool_append_handle(  | 
78  | 22  |        file_io_pool,  | 
79  | 22  |        &entry_index,  | 
80  | 22  |        file_io_handle,  | 
81  | 22  |        LIBBFIO_OPEN_READ,  | 
82  | 22  |        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  | 22  |   file_io_handle = NULL;  | 
89  |  |  | 
90  | 22  |   if( libsmraw_handle_initialize(  | 
91  | 22  |        &handle,  | 
92  | 22  |        NULL ) != 1 )  | 
93  | 0  |   { | 
94  | 0  |     goto on_error_libbfio;  | 
95  | 0  |   }  | 
96  | 22  |   if( libsmraw_handle_open_file_io_pool(  | 
97  | 22  |        handle,  | 
98  | 22  |        file_io_pool,  | 
99  | 22  |        LIBSMRAW_OPEN_READ,  | 
100  | 22  |        NULL ) != 1 )  | 
101  | 0  |   { | 
102  | 0  |     goto on_error_libsmraw;  | 
103  | 0  |   }  | 
104  | 22  |   libsmraw_handle_close(  | 
105  | 22  |    handle,  | 
106  | 22  |    NULL );  | 
107  |  |  | 
108  | 22  | on_error_libsmraw:  | 
109  | 22  |   libsmraw_handle_free(  | 
110  | 22  |    &handle,  | 
111  | 22  |    NULL );  | 
112  |  |  | 
113  | 22  | 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  | 22  |   if( file_io_pool != NULL )  | 
119  | 22  |   { | 
120  | 22  |     libbfio_pool_free(  | 
121  | 22  |      &file_io_pool,  | 
122  | 22  |      NULL );  | 
123  | 22  |   }  | 
124  | 22  |   if( file_io_handle != NULL )  | 
125  | 0  |   { | 
126  | 0  |     libbfio_handle_free(  | 
127  | 0  |      &file_io_handle,  | 
128  | 0  |      NULL );  | 
129  | 0  |   }  | 
130  | 22  |   return( 0 );  | 
131  | 22  | }  | 
132  |  |  | 
133  |  | } /* extern "C" */  | 
134  |  |  |