/src/openbabel/test/fuzz/fuzz_convert.cpp
Line | Count | Source |
1 | | #include <openbabel/babelconfig.h> |
2 | | #include <openbabel/mol.h> |
3 | | #include <openbabel/obconversion.h> |
4 | | #include <cstdlib> |
5 | | #include <stdio.h> |
6 | | #include <sstream> |
7 | | |
8 | | // Vendored alongside this file (LLVM Apache-2.0 WITH LLVM-exception) |
9 | | // so the harness compiles outside of an LLVM/libfuzzer toolchain — in |
10 | | // particular under GCC, where the libfuzzer headers aren't shipped. |
11 | | #include "FuzzedDataProvider.h" |
12 | | |
13 | 1 | std::vector<std::string> getAllFormats() { |
14 | 1 | std::vector<std::string> formats; |
15 | 1 | OpenBabel::OBPlugin::ListAsVector("formats", "ids", formats); |
16 | 1 | std::sort(formats.begin(), formats.end()); |
17 | 1 | return formats; |
18 | 1 | } |
19 | | |
20 | 16 | const char *randomFormat(FuzzedDataProvider &fdp) { |
21 | 16 | static std::vector<std::string> formats = getAllFormats(); |
22 | 16 | return formats[fdp.ConsumeIntegralInRange<int>(0, formats.size() - 1)].c_str(); |
23 | 16 | } |
24 | | |
25 | 8 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
26 | 8 | using namespace OpenBabel; |
27 | 8 | obErrorLog.StopLogging(); |
28 | | |
29 | 8 | FuzzedDataProvider fdp(Data, Size); |
30 | 8 | OBConversion conv; |
31 | 8 | conv.SetInFormat(randomFormat(fdp)); |
32 | 8 | conv.SetOutFormat(randomFormat(fdp)); |
33 | | |
34 | 8 | std::ostringstream outStream; |
35 | 8 | std::istringstream inStream(fdp.ConsumeRandomLengthString()); |
36 | | |
37 | 8 | try { |
38 | 8 | conv.Convert(&inStream, &outStream); |
39 | 8 | } catch (...) { |
40 | | // no error handling |
41 | 0 | } |
42 | | |
43 | 8 | return 0; |
44 | 8 | } |