/src/libcreg/ossfuzz/value_fuzzer.cc
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * OSS-Fuzz target for libcreg value 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  | 3.04k  | { | 
50  | 3.04k  |   libbfio_handle_t *file_io_handle = NULL;  | 
51  | 3.04k  |   libcreg_file_t *file             = NULL;  | 
52  | 3.04k  |   libcreg_key_t *root_key          = NULL;  | 
53  | 3.04k  |   libcreg_key_t *sub_key           = NULL;  | 
54  | 3.04k  |   libcreg_value_t *value           = NULL;  | 
55  | 3.04k  |   uint32_t value_type              = 0;  | 
56  | 3.04k  |   int number_of_sub_keys           = 0;  | 
57  | 3.04k  |   int number_of_values             = 0;  | 
58  |  |  | 
59  | 3.04k  |   if( libbfio_memory_range_initialize(  | 
60  | 3.04k  |        &file_io_handle,  | 
61  | 3.04k  |        NULL ) != 1 )  | 
62  | 0  |   { | 
63  | 0  |     return( 0 );  | 
64  | 0  |   }  | 
65  | 3.04k  |   if( libbfio_memory_range_set(  | 
66  | 3.04k  |        file_io_handle,  | 
67  | 3.04k  |        (uint8_t *) data,  | 
68  | 3.04k  |        size,  | 
69  | 3.04k  |        NULL ) != 1 )  | 
70  | 0  |   { | 
71  | 0  |     goto on_error_libbfio;  | 
72  | 0  |   }  | 
73  | 3.04k  |   if( libcreg_file_initialize(  | 
74  | 3.04k  |        &file,  | 
75  | 3.04k  |        NULL ) != 1 )  | 
76  | 0  |   { | 
77  | 0  |     goto on_error_libbfio;  | 
78  | 0  |   }  | 
79  | 3.04k  |   if( libcreg_file_open_file_io_handle(  | 
80  | 3.04k  |        file,  | 
81  | 3.04k  |        file_io_handle,  | 
82  | 3.04k  |        LIBCREG_OPEN_READ,  | 
83  | 3.04k  |        NULL ) != 1 )  | 
84  | 629  |   { | 
85  | 629  |     goto on_error_libcreg_file;  | 
86  | 629  |   }  | 
87  | 2.41k  |   if( libcreg_file_get_root_key(  | 
88  | 2.41k  |        file,  | 
89  | 2.41k  |        &root_key,  | 
90  | 2.41k  |        NULL ) == 1 )  | 
91  | 718  |   { | 
92  | 718  |     if( libcreg_key_get_number_of_sub_keys(  | 
93  | 718  |          root_key,  | 
94  | 718  |          &number_of_sub_keys,  | 
95  | 718  |          NULL ) != 1 )  | 
96  | 0  |     { | 
97  | 0  |       goto on_error_libcreg_root_key;  | 
98  | 0  |     }  | 
99  | 718  |     if( number_of_sub_keys > 0 )  | 
100  | 692  |     { | 
101  | 692  |       if( libcreg_key_get_sub_key_by_index(  | 
102  | 692  |            root_key,  | 
103  | 692  |            0,  | 
104  | 692  |            &sub_key,  | 
105  | 692  |            NULL ) != 1 )  | 
106  | 138  |       { | 
107  | 138  |         goto on_error_libcreg_root_key;  | 
108  | 138  |       }  | 
109  | 554  |       if( libcreg_key_get_number_of_values(  | 
110  | 554  |            sub_key,  | 
111  | 554  |            &number_of_values,  | 
112  | 554  |            NULL ) != 1 )  | 
113  | 0  |       { | 
114  | 0  |         goto on_error_libcreg_sub_key;  | 
115  | 0  |       }  | 
116  | 554  |       if( number_of_sub_keys > 0 )  | 
117  | 554  |       { | 
118  | 554  |         if( libcreg_key_get_value_by_index(  | 
119  | 554  |              root_key,  | 
120  | 554  |              0,  | 
121  | 554  |              &value,  | 
122  | 554  |              NULL ) != 1 )  | 
123  | 311  |         { | 
124  | 311  |           goto on_error_libcreg_sub_key;  | 
125  | 311  |         }  | 
126  | 243  |         libcreg_value_get_value_type(  | 
127  | 243  |          value,  | 
128  | 243  |          &value_type,  | 
129  | 243  |          NULL );  | 
130  |  |  | 
131  | 243  |         libcreg_value_free(  | 
132  | 243  |          &value,  | 
133  | 243  |          NULL );  | 
134  | 243  |       }  | 
135  | 554  | on_error_libcreg_sub_key:  | 
136  | 554  |       libcreg_key_free(  | 
137  | 554  |        &sub_key,  | 
138  | 554  |        NULL );  | 
139  | 554  |     }  | 
140  | 718  | on_error_libcreg_root_key:  | 
141  | 718  |     libcreg_key_free(  | 
142  | 718  |      &root_key,  | 
143  | 718  |      NULL );  | 
144  | 718  |   }  | 
145  | 2.41k  |   libcreg_file_close(  | 
146  | 2.41k  |    file,  | 
147  | 2.41k  |    NULL );  | 
148  |  |  | 
149  | 3.04k  | on_error_libcreg_file:  | 
150  | 3.04k  |   libcreg_file_free(  | 
151  | 3.04k  |    &file,  | 
152  | 3.04k  |    NULL );  | 
153  |  |  | 
154  | 3.04k  | on_error_libbfio:  | 
155  | 3.04k  |   libbfio_handle_free(  | 
156  | 3.04k  |    &file_io_handle,  | 
157  | 3.04k  |    NULL );  | 
158  |  |  | 
159  | 3.04k  |   return( 0 );  | 
160  | 3.04k  | }  | 
161  |  |  | 
162  |  | } /* extern "C" */  | 
163  |  |  |