Skip to content

Commit

Permalink
deal with option names in a config file that can be interpreted as se…
Browse files Browse the repository at this point in the history
…gment separators and subcommands.
  • Loading branch information
phlptp committed Nov 6, 2023
1 parent b457a85 commit 066fb94
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,10 @@ jobs:
cmake-version: "3.10"
if: success() || failure()

- name: Check CMake 3.11 (full)
- name: Check CMake 3.11
uses: ./.github/actions/quick_cmake
with:
cmake-version: "3.11"
args: -DCLI11_SANITIZERS=ON -DCLI11_BUILD_EXAMPLES_JSON=ON
if: success() || failure()

- name: Check CMake 3.12
Expand All @@ -255,6 +254,7 @@ jobs:
uses: ./.github/actions/quick_cmake
with:
cmake-version: "3.14"
args: -DCLI11_SANITIZERS=ON -DCLI11_BUILD_EXAMPLES_JSON=ON
if: success() || failure()

- name: Check CMake 3.15
Expand Down
3 changes: 2 additions & 1 deletion include/CLI/ConfigFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ struct ConfigItem {

/// This is the name
std::string name{};

/// @brief storage for the full name as a string
std::string orig_name{};
/// Listing of inputs
std::vector<std::string> inputs{};

Expand Down
55 changes: 37 additions & 18 deletions include/CLI/impl/App_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,14 +1396,26 @@ CLI11_INLINE void App::_parse_config(const std::vector<ConfigItem> &args) {
}

CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t level) {
Option *op{nullptr};
if(level < item.parents.size()) {
try {
auto *subcom = get_subcommand(item.parents.at(level));
auto result = subcom->_parse_single_config(item, level + 1);

return result;
} catch(const OptionNotFound &) {
return false;
//need to check for oddly named options first
if (item.orig_name != item.name)
{
op = get_option_no_throw("--" + item.orig_name);
if(op == nullptr) {
op = get_option_no_throw(item.orig_name);
}
}

if (op == nullptr)
{
try {
auto* subcom = get_subcommand(item.parents.at(level));
return subcom->_parse_single_config(item, level + 1);
}
catch (const OptionNotFound&) {
return false;
}
}
}
// check for section open
Expand All @@ -1426,15 +1438,20 @@ CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t
}
return true;
}
Option *op = get_option_no_throw("--" + item.name);
if(op == nullptr) {
if(item.name.size() == 1) {
op = get_option_no_throw("-" + item.name);
if (op == nullptr)
{
op = get_option_no_throw("--" + item.name);
if(op == nullptr) {
if(item.name.size() == 1) {
op = get_option_no_throw("-" + item.name);
}
if(op == nullptr) {
op = get_option_no_throw(item.name);
}
}
}
if(op == nullptr) {
op = get_option_no_throw(item.name);
}


if(op == nullptr) {
// If the option was not present
if(get_allow_config_extras() == config_extras_mode::capture)
Expand Down Expand Up @@ -1480,7 +1497,12 @@ CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t
if(op->get_items_expected_max() > 1) {
throw ArgumentMismatch::AtMost(item.fullname(), op->get_items_expected_max(), item.inputs.size());
}
if(op->get_disable_flag_override()) {

if (!op->get_disable_flag_override()) {
throw ConversionError::TooManyInputsFlag(item.fullname());
}
// if the disable flag override is set then we must have the flag values match a known flag value
// this is true regardless of the output value, so an array input is possible and must be accounted for
for(const auto &res : item.inputs) {
bool valid_value{false};
if(op->default_flag_values_.empty()) {
Expand All @@ -1503,9 +1525,6 @@ CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t
}
}
return true;
} else {
throw ConversionError::TooManyInputsFlag(item.fullname());
}
}
}
op->add_result(item.inputs);
Expand Down
6 changes: 5 additions & 1 deletion include/CLI/impl/Config_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
for(auto &it : items_buffer) {
detail::remove_quotes(it);
}

std::string orig_name=name;
std::vector<std::string> parents = detail::generate_parents(currentSection, name, parentSeparatorChar);
if(parents.size() > maximumLayers) {
continue;
Expand All @@ -357,6 +357,10 @@ inline std::vector<ConfigItem> ConfigBase::from_config(std::istream &input) cons
} else {
output.emplace_back();
output.back().parents = std::move(parents);
if (orig_name != name)
{
output.back().orig_name=std::move(orig_name);
}
output.back().name = std::move(name);
output.back().inputs = std::move(items_buffer);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
if(${CMAKE_VERSION} VERSION_GREATER "3.13.0")
if(CLI11_SANITIZERS)
message(STATUS "Using arsenm/sanitizers-cmake")
FetchContent_Declare(
sanitizers
GIT_REPOSITORY https://github.com/arsenm/sanitizers-cmake.git
GIT_SHALLOW 1
GIT_TAG c3dc841)
GIT_TAG 3f0542e)

FetchContent_GetProperties(sanitizers)

Expand All @@ -23,6 +24,7 @@ else()

endmacro()
endif()
endif()

# Add boost to test boost::optional (currently explicitly requested)"
option(CLI11_BOOST "Turn on boost test (currently may fail with Boost 1.70)" OFF)
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(6, 7));
int index = GENERATE(range(7, 8));
std::string optionString, flagString;
auto parseData = loadFailureFile("fuzz_app_file_fail", index);
if(parseData.size() > 25) {
Expand Down
3 changes: 3 additions & 0 deletions tests/fuzzFail/fuzz_app_file_fail7
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


.br-bN3CLI10ParseErrorEa5

0 comments on commit 066fb94

Please sign in to comment.