Skip to content

Commit

Permalink
style: pre-commit.ci fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Jul 9, 2023
1 parent 45f1bf6 commit acf7b0c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 43 deletions.
44 changes: 19 additions & 25 deletions include/CLI/TypeTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,11 +857,11 @@ inline std::string type_name() {

/// Convert to an unsigned integral
template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
bool integral_conversion(const std::string& input, T& output) noexcept {
if (input.empty() || input.front() == '-') {
bool integral_conversion(const std::string &input, T &output) noexcept {
if(input.empty() || input.front() == '-') {
return false;
}
char* val{nullptr};
char *val{nullptr};
errno = 0;
std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
if(errno == ERANGE) {
Expand Down Expand Up @@ -905,7 +905,7 @@ bool integral_conversion(const std::string &input, T &output) noexcept {
}

/// Convert a flag into an integer value typically binary flags sets errno to nonzero if conversion failed
inline std::int64_t to_flag_value(std::string val) noexcept{
inline std::int64_t to_flag_value(std::string val) noexcept {
static const std::string trueString("true");
static const std::string falseString("false");
if(val == trueString) {
Expand Down Expand Up @@ -933,7 +933,7 @@ inline std::int64_t to_flag_value(std::string val) noexcept{
ret = 1;
break;
default:
errno=EINVAL;
errno = EINVAL;
return -1;
}
return ret;
Expand All @@ -945,8 +945,8 @@ inline std::int64_t to_flag_value(std::string val) noexcept{
} else {
char *loc_ptr{nullptr};
ret = std::strtoll(val.c_str(), &loc_ptr, 0);
if(loc_ptr != (val.c_str() + val.size()) && errno==0) {
errno=EINVAL;
if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
errno = EINVAL;
}
}
return ret;
Expand Down Expand Up @@ -975,19 +975,14 @@ bool lexical_cast(const std::string &input, T &output) {
/// Boolean values
template <typename T,
enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
bool lexical_cast(const std::string& input, T& output) {
errno=0;
bool lexical_cast(const std::string &input, T &output) {
errno = 0;
auto out = to_flag_value(input);
if (errno==0)
{
if(errno == 0) {
output = (out > 0);
}
else if (errno==ERANGE)
{
} else if(errno == ERANGE) {
output = (input[0] != '-');
}
else
{
} else {
return false;
}
return true;
Expand Down Expand Up @@ -1646,14 +1641,13 @@ inline std::string sum_string_vector(const std::vector<std::string> &values) {
double tv{0.0};
auto comp = lexical_cast(arg, tv);
if(!comp) {
errno=0;
auto fv=detail::to_flag_value(arg);
fail=(errno!=0);
if (fail)
{
break;
}
tv = static_cast<double>(fv);
errno = 0;
auto fv = detail::to_flag_value(arg);
fail = (errno != 0);
if(fail) {
break;
}
tv = static_cast<double>(fv);
}
val += tv;
}
Expand Down
6 changes: 3 additions & 3 deletions include/CLI/impl/Option_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ CLI11_NODISCARD CLI11_INLINE std::string Option::get_flag_value(const std::strin
return input_value;
}
if(default_flag_values_[static_cast<std::size_t>(ind)].second == falseString) {
errno=0;
errno = 0;
auto val = detail::to_flag_value(input_value);
if (errno != 0) {
errno=0;
if(errno != 0) {
errno = 0;
return input_value;
}
return (val == 1) ? falseString : (val == (-1) ? trueString : std::to_string(-val));
Expand Down
9 changes: 4 additions & 5 deletions tests/FuzzFailTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ TEST_CASE("app_fail") {
}
}


TEST_CASE("file_fail") {
CLI::FuzzApp fuzzdata;
auto app = fuzzdata.generateApp();

int index = GENERATE(range(1, 2));
auto parseData = loadFailureFile("fuzz_file_fail", index);
std::stringstream out(parseData);
try {
app->parse_from_stream(out);
} catch(const CLI::ParseError & /*e*/) {
}
try {
app->parse_from_stream(out);
} catch(const CLI::ParseError & /*e*/) {
}
}
19 changes: 10 additions & 9 deletions tests/HelpersTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,24 @@ TEST_CASE("StringTools: Modify3", "[helpers]") {

TEST_CASE("StringTools: flagValues", "[helpers]") {
CHECK(-1 == CLI::detail::to_flag_value("0"));
CHECK(errno==0);
CHECK(errno == 0);
CHECK(1 == CLI::detail::to_flag_value("t"));
CHECK(1 == CLI::detail::to_flag_value("1"));
CHECK(6 == CLI::detail::to_flag_value("6"));
CHECK(-6 == CLI::detail::to_flag_value("-6"));
CHECK(-1 == CLI::detail::to_flag_value("false"));
CHECK(1 == CLI::detail::to_flag_value("YES"));
errno=0;
errno = 0;
CLI::detail::to_flag_value("frog");
CHECK(errno==EINVAL);
errno=0;
CHECK(errno == EINVAL);
errno = 0;
CLI::detail::to_flag_value("q");
CHECK(errno==EINVAL);
errno=0;
CLI::detail::to_flag_value("77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
CHECK(errno==ERANGE);
errno=0;
CHECK(errno == EINVAL);
errno = 0;
CLI::detail::to_flag_value(
"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
CHECK(errno == ERANGE);
errno = 0;
CHECK(-1 == CLI::detail::to_flag_value("NO"));
CHECK(475555233 == CLI::detail::to_flag_value("475555233"));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fuzzFail/fuzz_file_fail1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nflag2=555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555"="
nflag2=555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555"="

0 comments on commit acf7b0c

Please sign in to comment.