/src/libfsfat/ossfuzz/file_entry_fuzzer.cc
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * OSS-Fuzz target for libfsfat file_entry type  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2021-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_libfsfat.h"  | 
31  |  |  | 
32  |  | #if !defined( LIBFSFAT_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  |  | LIBFSFAT_EXTERN \  | 
38  |  | int libfsfat_volume_open_file_io_handle(  | 
39  |  |      libfsfat_volume_t *volume,  | 
40  |  |      libbfio_handle_t *file_io_handle,  | 
41  |  |      int access_flags,  | 
42  |  |      libfsfat_error_t **error );  | 
43  |  |  | 
44  |  | #endif /* !defined( LIBFSFAT_HAVE_BFIO ) */  | 
45  |  |  | 
46  |  | int LLVMFuzzerTestOneInput(  | 
47  |  |      const uint8_t *data,  | 
48  |  |      size_t size )  | 
49  | 22.3k  | { | 
50  | 22.3k  |   libbfio_handle_t *file_io_handle      = NULL;  | 
51  | 22.3k  |   libfsfat_file_entry_t *root_directory = NULL;  | 
52  | 22.3k  |   libfsfat_file_entry_t *sub_file_entry = NULL;  | 
53  | 22.3k  |   libfsfat_volume_t *volume             = NULL;  | 
54  | 22.3k  |   int number_of_sub_file_entries        = 0;  | 
55  |  |  | 
56  | 22.3k  |   if( libbfio_memory_range_initialize(  | 
57  | 22.3k  |        &file_io_handle,  | 
58  | 22.3k  |        NULL ) != 1 )  | 
59  | 0  |   { | 
60  | 0  |     return( 0 );  | 
61  | 0  |   }  | 
62  | 22.3k  |   if( libbfio_memory_range_set(  | 
63  | 22.3k  |        file_io_handle,  | 
64  | 22.3k  |        (uint8_t *) data,  | 
65  | 22.3k  |        size,  | 
66  | 22.3k  |        NULL ) != 1 )  | 
67  | 0  |   { | 
68  | 0  |     goto on_error_libbfio;  | 
69  | 0  |   }  | 
70  | 22.3k  |   if( libfsfat_volume_initialize(  | 
71  | 22.3k  |        &volume,  | 
72  | 22.3k  |        NULL ) != 1 )  | 
73  | 0  |   { | 
74  | 0  |     goto on_error_libbfio;  | 
75  | 0  |   }  | 
76  | 22.3k  |   if( libfsfat_volume_open_file_io_handle(  | 
77  | 22.3k  |        volume,  | 
78  | 22.3k  |        file_io_handle,  | 
79  | 22.3k  |        LIBFSFAT_OPEN_READ,  | 
80  | 22.3k  |        NULL ) != 1 )  | 
81  | 12.1k  |   { | 
82  | 12.1k  |     goto on_error_libfsfat_volume;  | 
83  | 12.1k  |   }  | 
84  | 10.2k  |   if( libfsfat_volume_get_root_directory(  | 
85  | 10.2k  |        volume,  | 
86  | 10.2k  |        &root_directory,  | 
87  | 10.2k  |        NULL ) == 1 )  | 
88  | 2.91k  |   { | 
89  | 2.91k  |     if( libfsfat_file_entry_get_number_of_sub_file_entries(  | 
90  | 2.91k  |          root_directory,  | 
91  | 2.91k  |          &number_of_sub_file_entries,  | 
92  | 2.91k  |          NULL ) != 1 )  | 
93  | 1.73k  |     { | 
94  | 1.73k  |       goto on_error_libfsfat_root_directory;  | 
95  | 1.73k  |     }  | 
96  | 1.17k  |     if( number_of_sub_file_entries > 0 )  | 
97  | 698  |     { | 
98  | 698  |       if( libfsfat_file_entry_get_sub_file_entry_by_index(  | 
99  | 698  |            root_directory,  | 
100  | 698  |            0,  | 
101  | 698  |            &sub_file_entry,  | 
102  | 698  |            NULL ) != 1 )  | 
103  | 202  |       { | 
104  | 202  |         goto on_error_libfsfat_root_directory;  | 
105  | 202  |       }  | 
106  | 496  |       libfsfat_file_entry_free(  | 
107  | 496  |        &sub_file_entry,  | 
108  | 496  |        NULL );  | 
109  | 496  |     }  | 
110  | 2.91k  | on_error_libfsfat_root_directory:  | 
111  | 2.91k  |     libfsfat_file_entry_free(  | 
112  | 2.91k  |      &root_directory,  | 
113  | 2.91k  |      NULL );  | 
114  | 2.91k  |   }  | 
115  | 10.2k  |   libfsfat_volume_close(  | 
116  | 10.2k  |    volume,  | 
117  | 10.2k  |    NULL );  | 
118  |  |  | 
119  | 22.3k  | on_error_libfsfat_volume:  | 
120  | 22.3k  |   libfsfat_volume_free(  | 
121  | 22.3k  |    &volume,  | 
122  | 22.3k  |    NULL );  | 
123  |  |  | 
124  | 22.3k  | on_error_libbfio:  | 
125  | 22.3k  |   libbfio_handle_free(  | 
126  | 22.3k  |    &file_io_handle,  | 
127  | 22.3k  |    NULL );  | 
128  |  |  | 
129  | 22.3k  |   return( 0 );  | 
130  | 22.3k  | }  | 
131  |  |  | 
132  |  | } /* extern "C" */  | 
133  |  |  |