Skip to content

Commit 45496a8

Browse files
phlptphenryiii
authored andcommitted
The add subcommand function was not exception safe. It added the subcommand to the vector before checking the already added option. This would result in duplicate subcommands being in place in the subcommands_ vector. The modifications make it exception safe and remove what I think was an unnecessary check for pointer duplication, that as far as I can tell was always false since it was comparing a newly created pointer directly to previously created ones.
1 parent 478f582 commit 45496a8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

include/CLI/App.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,11 +1080,11 @@ class App {
10801080

10811081
/// Add a subcommand. Inherits INHERITABLE and OptionDefaults, and help flag
10821082
App *add_subcommand(std::string subcommand_name, std::string description = "") {
1083-
subcommands_.emplace_back(new App(description, subcommand_name, this));
1083+
CLI::App_p subcom(new App(description, subcommand_name, this));
10841084
for(const auto &subc : subcommands_)
1085-
if(subc.get() != subcommands_.back().get())
1086-
if(subc->check_name(subcommands_.back()->name_) || subcommands_.back()->check_name(subc->name_))
1087-
throw OptionAlreadyAdded(subc->name_);
1085+
if(subc->check_name(subcommand_name) || subcom->check_name(subc->name_))
1086+
throw OptionAlreadyAdded(subc->name_);
1087+
subcommands_.push_back(std::move(subcom));
10881088
return subcommands_.back().get();
10891089
}
10901090

0 commit comments

Comments
 (0)