/src/inchi_input_fuzzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 Google Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <stddef.h> |
16 | | #include <stdint.h> |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include "inchi_api.h" |
21 | | |
22 | | // Define the maximum value for size_t. We return if the fuzzing input is equal |
23 | | // to kSizeMax because appending the null-terminator to the InChI buffer would |
24 | | // cause wraparound, thereby initializing the buffer to size 0. |
25 | | static const size_t kSizeMax = (size_t)-1; |
26 | | |
27 | 288 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
28 | | |
29 | 288 | if (size == kSizeMax) |
30 | 0 | return 0; |
31 | | |
32 | 288 | char *szINCHISource = malloc(sizeof(char) * (size + 1)); |
33 | 288 | memcpy(szINCHISource, data, size); |
34 | 288 | szINCHISource[size] = '\0'; // InChI string must be null-terminated |
35 | | |
36 | | // Buffer lengths taken from InChI API reference, located at |
37 | | // https://www.inchi-trust.org/download/104/InChI_API_Reference.pdf, page 24 |
38 | 288 | char szINCHIKey[28], szXtra1[65], szXtra2[65]; |
39 | 288 | GetINCHIKeyFromINCHI(szINCHISource, 0, 0, szINCHIKey, szXtra1, szXtra2); |
40 | | |
41 | 288 | inchi_InputINCHI inpInChI; |
42 | 288 | inpInChI.szInChI = szINCHISource; |
43 | 288 | inpInChI.szOptions = NULL; |
44 | | |
45 | 288 | inchi_Output out; |
46 | 288 | GetINCHIfromINCHI(&inpInChI, &out); |
47 | | |
48 | 288 | inchi_OutputStruct outStruct; |
49 | 288 | GetStructFromINCHI(&inpInChI, &outStruct); |
50 | | |
51 | 288 | free(szINCHISource); |
52 | 288 | FreeINCHI(&out); |
53 | 288 | FreeStructFromINCHI(&outStruct); |
54 | | |
55 | 288 | return 0; |
56 | 288 | } |