From 2052b225d9438328b27acee35a54e3ce1a5ec43e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 29 Jan 2017 13:56:05 +0000 Subject: [PATCH] Detect too deep command call stack Fixes #1163 --- src/command_manager.cc | 9 ++++++++- src/command_manager.hh | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 6b79df3c..d8c5bc58 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -417,11 +417,18 @@ CommandManager::find_command(const Context& context, const String& name) const void CommandManager::execute_single_command(CommandParameters params, Context& context, const ShellContext& shell_context, - DisplayCoord pos) const + DisplayCoord pos) { if (params.empty()) 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()); auto command_it = find_command(context, params[0]); if (command_it == m_commands.end()) diff --git a/src/command_manager.hh b/src/command_manager.hh index 55d62921..7ef10f2c 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -112,7 +112,7 @@ private: void execute_single_command(CommandParameters params, Context& context, const ShellContext& shell_context, - DisplayCoord pos) const; + DisplayCoord pos); struct CommandDescriptor { @@ -126,6 +126,7 @@ private: using CommandMap = UnorderedMap; CommandMap m_commands; String m_last_complete_command; + int m_command_depth = 0; CommandMap::const_iterator find_command(const Context& context, const String& name) const;