Detect too deep command call stack

Fixes #1163
This commit is contained in:
Maxime Coste 2017-01-29 13:56:05 +00:00
parent 753f3a50d1
commit 2052b225d9
2 changed files with 10 additions and 2 deletions

View File

@ -417,11 +417,18 @@ CommandManager::find_command(const Context& context, const String& name) const
void CommandManager::execute_single_command(CommandParameters params, void CommandManager::execute_single_command(CommandParameters params,
Context& context, Context& context,
const ShellContext& shell_context, const ShellContext& shell_context,
DisplayCoord pos) const DisplayCoord pos)
{ {
if (params.empty()) if (params.empty())
return; return;
constexpr int max_command_depth = 100;
if (m_command_depth > max_command_depth)
throw runtime_error("maximum nested command depth hit");
++m_command_depth;
auto pop_cmd = on_scope_end([this] { --m_command_depth; });
ParameterList param_view(params.begin()+1, params.end()); ParameterList param_view(params.begin()+1, params.end());
auto command_it = find_command(context, params[0]); auto command_it = find_command(context, params[0]);
if (command_it == m_commands.end()) if (command_it == m_commands.end())

View File

@ -112,7 +112,7 @@ private:
void execute_single_command(CommandParameters params, void execute_single_command(CommandParameters params,
Context& context, Context& context,
const ShellContext& shell_context, const ShellContext& shell_context,
DisplayCoord pos) const; DisplayCoord pos);
struct CommandDescriptor struct CommandDescriptor
{ {
@ -126,6 +126,7 @@ private:
using CommandMap = UnorderedMap<String, CommandDescriptor, MemoryDomain::Commands>; using CommandMap = UnorderedMap<String, CommandDescriptor, MemoryDomain::Commands>;
CommandMap m_commands; CommandMap m_commands;
String m_last_complete_command; String m_last_complete_command;
int m_command_depth = 0;
CommandMap::const_iterator find_command(const Context& context, CommandMap::const_iterator find_command(const Context& context,
const String& name) const; const String& name) const;