| 11 | #include <string> |
| 12 | |
| 13 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
| 14 | if(Size == 0) { |
| 15 | return 0; |
| 16 | } |
| 17 | std::string parseString(reinterpret_cast<const char *>(Data), Size); |
| 18 | |
| 19 | CLI::FuzzApp fuzzdata; |
| 20 | |
| 21 | auto app = fuzzdata.generateApp(); |
| 22 | std::size_t pstring_start{0}; |
| 23 | try { |
| 24 | pstring_start = fuzzdata.add_custom_options(app.get(), parseString); |
| 25 | } catch(const CLI::ConstructionError &e) { |
| 26 | return 0; // Non-zero return values are reserved for future use. |
| 27 | } |
| 28 | |
| 29 | try { |
| 30 | if(pstring_start > 0) { |
| 31 | app->parse(parseString.substr(pstring_start)); |
| 32 | // test to make sure no seg fault or other errors occur |
| 33 | std::ignore = app->help("", CLI::AppFormatMode::All); |
| 34 | } else { |
| 35 | app->parse(parseString); |
| 36 | } |
| 37 | } catch(const CLI::HorribleError &he) { |
| 38 | throw; |
| 39 | } catch(const CLI::ParseError &e) { |
| 40 | //(app)->exit(e); |
| 41 | // this just indicates we caught an error known by CLI |
| 42 | return 0; // Non-zero return values are reserved for future use. |
| 43 | } |
| 44 | if(fuzzdata.supports_config_file()) { |
| 45 | CLI::FuzzApp fuzzdata2; |
| 46 | auto app2 = fuzzdata2.generateApp(); |
| 47 | // should be able to write the config to a file and read from it again |
| 48 | std::string configOut = app->config_to_str(); |
| 49 | std::stringstream out(configOut); |
| 50 | if(pstring_start > 0) { |
| 51 | fuzzdata2.add_custom_options(app2.get(), parseString); |
| 52 | } |
| 53 | app2->parse_from_stream(out); |
| 54 | auto result = fuzzdata2.compare(fuzzdata); |
| 55 | if(!result) { |
| 56 | throw CLI::ValidationError("fuzzer", "file input results don't match parse results"); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
nothing calls this directly
no test coverage detected