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>
This commit is contained in:
Philip Top
2025-08-28 17:11:03 -07:00
committed by GitHub
parent 399e729a55
commit f7e4695ed8
3 changed files with 38 additions and 1 deletions

View File

@@ -4208,3 +4208,24 @@ TEST_CASE_METHOD(TApp, "RoundTripArrayFloat", "[config]") {
CHECK(cv[0] == -1.0F);
CHECK(cv[1] == 1.0F);
}
// Code from https://github.com/CLIUtils/CLI11/issues/1197
TEST_CASE_METHOD(TApp, "CrashTest", "[config]") {
args = {"spdlog", "--level=off"};
app.configurable()->allow_config_extras(false);
app.set_config("--conf")->check(CLI::ExistingFile);
std::string level;
auto *command = app.add_subcommand("spdlog");
command->add_option("--level", level, "Log level")->default_val("info");
run();
auto *ptr = app.get_config_ptr();
std::string conf_filename;
CHECK_NOTHROW(conf_filename = ptr->as<std::string>());
CHECK(conf_filename.empty());
CHECK(level == "off");
}

View File

@@ -1516,6 +1516,14 @@ TEST_CASE("Types: LexicalConversionVectorDouble", "[helpers]") {
CHECK(-3.54 == Approx(x[2]));
}
TEST_CASE("Types: LexicalConversionEmptyVectorDouble", "[helpers]") {
CLI::results_t input = {};
std::vector<double> x;
bool res = CLI::detail::lexical_conversion<std::vector<double>, std::vector<double>>(input, x);
CHECK(res);
CHECK(x.empty());
}
static_assert(!CLI::detail::is_tuple_like<std::vector<double>>::value, "vector should not be like a tuple");
static_assert(CLI::detail::is_tuple_like<std::pair<double, double>>::value, "pair of double should be like a tuple");
static_assert(CLI::detail::is_tuple_like<std::array<double, 4>>::value, "std::array<double,4> should be like a tuple");