Skip to content

Commit

Permalink
get fuzzing test to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Nov 11, 2023
1 parent 8c14910 commit 292d907
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/CLI/impl/App_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t
op->add_result(res);
return true;
}
if(static_cast<int>(item.inputs.size()) > op->get_items_expected_max()) {
if(static_cast<int>(item.inputs.size()) > op->get_items_expected_max() && op->get_multi_option_policy()!=MultiOptionPolicy::TakeAll) {
if(op->get_items_expected_max() > 1) {
throw ArgumentMismatch::AtMost(item.fullname(), op->get_items_expected_max(), item.inputs.size());
}
Expand Down
34 changes: 26 additions & 8 deletions include/CLI/impl/Config_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,35 @@ namespace detail {
// check if a given character is printable
// the cast is necessary to avoid undefined behaviour
if (isprint((unsigned char)c) == 0) {
escaped_string.push_back(c);
}
else
{
std::stringstream stream;
// if the character is not printable
// we'll convert it to a hex string using a stringstream
// note that since char is signed we have to cast it to unsigned first
stream << std::hex << (unsigned int)(unsigned char)(c);
std::string code = stream.str();
escaped_string += std::string("\\x")+(code.size()<2?"0":"")+code;

}
else
{
escaped_string.push_back(c);
}
}
}

if (escaped_string != string_to_escape)
{
escaped_string.insert(0,"B(\"");
escaped_string.push_back(')');
escaped_string.push_back('"');
}
return escaped_string;
}

CLI11_INLINE bool is_printable(const std::string& test_string)
{
return std::all_of(test_string.begin(), test_string.end(), [](char x) {return (isprint(static_cast<unsigned char>(x)) != 0 || x == '\n'); });
}

CLI11_INLINE std::string convert_arg_for_ini(const std::string &arg, char stringQuote, char characterQuote) {
static const std::string elchars=std::string("\n")+'\0';
if(arg.empty()) {
return std::string(2, stringQuote);
}
Expand All @@ -69,6 +79,10 @@ CLI11_INLINE std::string convert_arg_for_ini(const std::string &arg, char string
}
// just quote a single non numeric character
if(arg.size() == 1) {
if (isprint(static_cast<unsigned char>(arg.front())) == 0)
{
return escape_string(arg);
}
return std::string(1, characterQuote) + arg + characterQuote;
}
// handle hex, binary or octal arguments
Expand All @@ -89,7 +103,11 @@ CLI11_INLINE std::string convert_arg_for_ini(const std::string &arg, char string
}
}
}
if(arg.find_first_of(elchars) != std::string::npos) {
if (!is_printable(arg))
{
return escape_string(arg);
}
if (arg.find_first_of("\n") != std::string::npos) {
return std::string(tquote) + arg + tquote;
}
if(arg.find_first_of(stringQuote) == std::string::npos) {
Expand Down
2 changes: 1 addition & 1 deletion tests/FuzzFailTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST_CASE("app_file_gen_fail") {
CLI::FuzzApp fuzzdata;
auto app = fuzzdata.generateApp();

int index = GENERATE(range(8, 9));
int index = GENERATE(range(1, 9));
std::string optionString, flagString;
auto parseData = loadFailureFile("fuzz_app_file_fail", index);
if(parseData.size() > 25) {
Expand Down

0 comments on commit 292d907

Please sign in to comment.