1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-02 21:53:51 +00:00

fix: a potential stack overflow error (#665)

* Fix a potential stack overflow error if nameless subcommands have fallthough

* style: pre-commit.ci fixes

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Philip Top 2021-11-17 21:44:10 -08:00 committed by GitHub
parent 815553211b
commit eba619fc68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -247,3 +247,8 @@ set_property(TEST retired_deprecated PROPERTY PASS_REGULAR_EXPRESSION "deprecate
add_cli_exe(custom_parse custom_parse.cpp)
add_test(NAME cp_test COMMAND custom_parse --dv 1.7)
set_property(TEST cp_test PROPERTY PASS_REGULAR_EXPRESSION "called correct")
#------------------------------------------------
# This executable is for manual testing and is expected to change regularly
add_cli_exe(tester testEXE.cpp)

26
examples/testEXE.cpp Normal file
View File

@ -0,0 +1,26 @@
// Copyright (c) 2017-2021, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
// Code modified from https://github.com/CLIUtils/CLI11/issues/559
#include <CLI/CLI.hpp>
#include <iostream>
#include <string>
int main(int argc, const char *argv[]) {
int logLevel{0};
CLI::App app{"Test App"};
app.add_option("-v", logLevel, "level");
auto subcom = app.add_subcommand("sub", "")->fallthrough();
subcom->preparse_callback([&app](size_t) { app.get_subcommand("sub")->add_option_group("group"); });
CLI11_PARSE(app, argc, argv);
std::cout << "level: " << logLevel << std::endl;
}

View File

@ -2732,13 +2732,16 @@ class App {
}
}
}
// If a subcommand, try the main command
if(parent_ != nullptr && fallthrough_)
return _get_fallthrough_parent()->_parse_arg(args, current_type);
// don't capture missing if this is a nameless subcommand
// don't capture missing if this is a nameless subcommand and nameless subcommands can't fallthrough
if(parent_ != nullptr && name_.empty()) {
return false;
}
// If a subcommand, try the main command
if(parent_ != nullptr && fallthrough_)
return _get_fallthrough_parent()->_parse_arg(args, current_type);
// Otherwise, add to missing
args.pop_back();
_move_to_missing(current_type, current);