From 48839a9ea355e2b021a292ec41e688599df24421 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Sat, 17 Sep 2016 11:31:04 +0200 Subject: [PATCH] Fix crashes with unknown/invalid commands --- src/anbox/cli.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/anbox/cli.cpp b/src/anbox/cli.cpp index bc7e6df9..fc33db4d 100644 --- a/src/anbox/cli.cpp +++ b/src/anbox/cli.cpp @@ -141,8 +141,10 @@ void cli::CommandWithSubcommands::help(std::ostream& out) if (commands_.size() > 0) { out << std::endl << pattern::commands << std::endl; - for (const auto& cmd : commands_) - out << boost::format(pattern::command) % cmd.second->name() % cmd.second->description() << std::endl; + for (const auto& cmd : commands_) { + if (cmd.second) + out << boost::format(pattern::command) % cmd.second->name() % cmd.second->description() << std::endl; + } } } @@ -169,7 +171,15 @@ int cli::CommandWithSubcommands::run(const cli::Command::Context& ctxt) po::store(parsed, vm); po::notify(vm); - return commands_[vm["command"].as()]->run(cli::Command::Context + auto cmd = commands_[vm["command"].as()]; + if (!cmd) + { + ctxt.cout << "Unknown command '" << vm["command"].as() << "'" << std::endl; + help(ctxt.cout); + return EXIT_FAILURE; + } + + return cmd->run(cli::Command::Context { ctxt.cin, ctxt.cout, -- GitLab