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
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)27 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
28
29 if (size == kSizeMax)
30 return 0;
31
32 char *szINCHISource = malloc(sizeof(char) * (size + 1));
33 memcpy(szINCHISource, data, size);
34 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 char szINCHIKey[28], szXtra1[65], szXtra2[65];
39 GetINCHIKeyFromINCHI(szINCHISource, 0, 0, szINCHIKey, szXtra1, szXtra2);
40
41 inchi_InputINCHI inpInChI;
42 inpInChI.szInChI = szINCHISource;
43
44 inchi_Output out;
45 GetINCHIfromINCHI(&inpInChI, &out);
46
47 inchi_OutputStruct outStruct;
48 GetStructFromINCHI(&inpInChI, &outStruct);
49
50 free(szINCHISource);
51 FreeINCHI(&out);
52 FreeStructFromINCHI(&outStruct);
53
54 return 0;
55 }
56