/src/libcreg/ossfuzz/key_fuzzer.cc
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * OSS-Fuzz target for libcreg key type  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2013-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_libcreg.h"  | 
31  |  |  | 
32  |  | #if !defined( LIBCREG_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  |  | LIBCREG_EXTERN \  | 
38  |  | int libcreg_file_open_file_io_handle(  | 
39  |  |      libcreg_file_t *file,  | 
40  |  |      libbfio_handle_t *file_io_handle,  | 
41  |  |      int access_flags,  | 
42  |  |      libcreg_error_t **error );  | 
43  |  |  | 
44  |  | #endif /* !defined( LIBCREG_HAVE_BFIO ) */  | 
45  |  |  | 
46  |  | int LLVMFuzzerTestOneInput(  | 
47  |  |      const uint8_t *data,  | 
48  |  |      size_t size )  | 
49  | 1.22k  | { | 
50  | 1.22k  |   libbfio_handle_t *file_io_handle = NULL;  | 
51  | 1.22k  |   libcreg_file_t *file             = NULL;  | 
52  | 1.22k  |   libcreg_key_t *root_key          = NULL;  | 
53  | 1.22k  |   libcreg_key_t *sub_key           = NULL;  | 
54  | 1.22k  |   int number_of_sub_keys           = 0;  | 
55  |  |  | 
56  | 1.22k  |   if( libbfio_memory_range_initialize(  | 
57  | 1.22k  |        &file_io_handle,  | 
58  | 1.22k  |        NULL ) != 1 )  | 
59  | 0  |   { | 
60  | 0  |     return( 0 );  | 
61  | 0  |   }  | 
62  | 1.22k  |   if( libbfio_memory_range_set(  | 
63  | 1.22k  |        file_io_handle,  | 
64  | 1.22k  |        (uint8_t *) data,  | 
65  | 1.22k  |        size,  | 
66  | 1.22k  |        NULL ) != 1 )  | 
67  | 0  |   { | 
68  | 0  |     goto on_error_libbfio;  | 
69  | 0  |   }  | 
70  | 1.22k  |   if( libcreg_file_initialize(  | 
71  | 1.22k  |        &file,  | 
72  | 1.22k  |        NULL ) != 1 )  | 
73  | 0  |   { | 
74  | 0  |     goto on_error_libbfio;  | 
75  | 0  |   }  | 
76  | 1.22k  |   if( libcreg_file_open_file_io_handle(  | 
77  | 1.22k  |        file,  | 
78  | 1.22k  |        file_io_handle,  | 
79  | 1.22k  |        LIBCREG_OPEN_READ,  | 
80  | 1.22k  |        NULL ) != 1 )  | 
81  | 214  |   { | 
82  | 214  |     goto on_error_libcreg_file;  | 
83  | 214  |   }  | 
84  | 1.00k  |   if( libcreg_file_get_root_key(  | 
85  | 1.00k  |        file,  | 
86  | 1.00k  |        &root_key,  | 
87  | 1.00k  |        NULL ) == 1 )  | 
88  | 251  |   { | 
89  | 251  |     if( libcreg_key_get_number_of_sub_keys(  | 
90  | 251  |          root_key,  | 
91  | 251  |          &number_of_sub_keys,  | 
92  | 251  |          NULL ) != 1 )  | 
93  | 0  |     { | 
94  | 0  |       goto on_error_libcreg_root_key;  | 
95  | 0  |     }  | 
96  | 251  |     if( number_of_sub_keys > 0 )  | 
97  | 249  |     { | 
98  | 249  |       if( libcreg_key_get_sub_key_by_index(  | 
99  | 249  |            root_key,  | 
100  | 249  |            0,  | 
101  | 249  |            &sub_key,  | 
102  | 249  |            NULL ) != 1 )  | 
103  | 108  |       { | 
104  | 108  |         goto on_error_libcreg_root_key;  | 
105  | 108  |       }  | 
106  | 141  |       libcreg_key_free(  | 
107  | 141  |        &sub_key,  | 
108  | 141  |        NULL );  | 
109  | 141  |     }  | 
110  | 251  | on_error_libcreg_root_key:  | 
111  | 251  |     libcreg_key_free(  | 
112  | 251  |      &root_key,  | 
113  | 251  |      NULL );  | 
114  | 251  |   }  | 
115  | 1.00k  |   libcreg_file_close(  | 
116  | 1.00k  |    file,  | 
117  | 1.00k  |    NULL );  | 
118  |  |  | 
119  | 1.22k  | on_error_libcreg_file:  | 
120  | 1.22k  |   libcreg_file_free(  | 
121  | 1.22k  |    &file,  | 
122  | 1.22k  |    NULL );  | 
123  |  |  | 
124  | 1.22k  | on_error_libbfio:  | 
125  | 1.22k  |   libbfio_handle_free(  | 
126  | 1.22k  |    &file_io_handle,  | 
127  | 1.22k  |    NULL );  | 
128  |  |  | 
129  | 1.22k  |   return( 0 );  | 
130  | 1.22k  | }  | 
131  |  |  | 
132  |  | } /* extern "C" */  | 
133  |  |  |