Skip to content

Commit f7e4695

Browse files
fix edge case with config files pointer (#1199)
generating a seg fault if no default and no config file provided. Fixes #1197 This was likely introduced by the combination of fixes for some issues with the config parsing and some updates to the as<T> method a while back. This edge case on the handling of the config pointer with as was not overlooked in the earlier testing. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 399e729 commit f7e4695

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

include/CLI/Option.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,15 @@ class Option : public OptionBase<Option> {
681681
bool retval = false;
682682
if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
683683
const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
684-
retval = detail::lexical_conversion<T, T>(res, output);
684+
if(!res.empty()) {
685+
retval = detail::lexical_conversion<T, T>(res, output);
686+
} else {
687+
results_t res2;
688+
res2.emplace_back();
689+
proc_results_ = std::move(res2);
690+
retval = detail::lexical_conversion<T, T>(proc_results_, output);
691+
}
692+
685693
} else {
686694
results_t res;
687695
if(results_.empty()) {

tests/ConfigFileTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4208,3 +4208,24 @@ TEST_CASE_METHOD(TApp, "RoundTripArrayFloat", "[config]") {
42084208
CHECK(cv[0] == -1.0F);
42094209
CHECK(cv[1] == 1.0F);
42104210
}
4211+
4212+
// Code from https://github.com/CLIUtils/CLI11/issues/1197
4213+
TEST_CASE_METHOD(TApp, "CrashTest", "[config]") {
4214+
args = {"spdlog", "--level=off"};
4215+
4216+
app.configurable()->allow_config_extras(false);
4217+
app.set_config("--conf")->check(CLI::ExistingFile);
4218+
4219+
std::string level;
4220+
4221+
auto *command = app.add_subcommand("spdlog");
4222+
command->add_option("--level", level, "Log level")->default_val("info");
4223+
4224+
run();
4225+
4226+
auto *ptr = app.get_config_ptr();
4227+
std::string conf_filename;
4228+
CHECK_NOTHROW(conf_filename = ptr->as<std::string>());
4229+
CHECK(conf_filename.empty());
4230+
CHECK(level == "off");
4231+
}

tests/HelpersTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,14 @@ TEST_CASE("Types: LexicalConversionVectorDouble", "[helpers]") {
15161516
CHECK(-3.54 == Approx(x[2]));
15171517
}
15181518

1519+
TEST_CASE("Types: LexicalConversionEmptyVectorDouble", "[helpers]") {
1520+
CLI::results_t input = {};
1521+
std::vector<double> x;
1522+
bool res = CLI::detail::lexical_conversion<std::vector<double>, std::vector<double>>(input, x);
1523+
CHECK(res);
1524+
CHECK(x.empty());
1525+
}
1526+
15191527
static_assert(!CLI::detail::is_tuple_like<std::vector<double>>::value, "vector should not be like a tuple");
15201528
static_assert(CLI::detail::is_tuple_like<std::pair<double, double>>::value, "pair of double should be like a tuple");
15211529
static_assert(CLI::detail::is_tuple_like<std::array<double, 4>>::value, "std::array<double,4> should be like a tuple");

0 commit comments

Comments
 (0)