From e3cd820fb13544862e7ed423d12362a5f19505c5 Mon Sep 17 00:00:00 2001 From: lethosor Date: Wed, 12 Jul 2017 16:28:21 -0400 Subject: [PATCH] Limit recursive runCommand() calls to 20 Addresses an issue with recursive aliases crashing (#701) --- library/Core.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/Core.cpp b/library/Core.cpp index 0f6db80f9..fccf7f464 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -135,6 +135,16 @@ struct Core::Private } }; +struct CommandDepthCounter +{ + static const int MAX_DEPTH = 20; + static thread_local int depth; + CommandDepthCounter() { depth++; } + ~CommandDepthCounter() { depth--; } + bool ok() { return depth < MAX_DEPTH; } +}; +thread_local int CommandDepthCounter::depth = 0; + void Core::cheap_tokenise(string const& input, vector &output) { string *cur = NULL; @@ -651,6 +661,14 @@ void ls_helper(color_ostream &con, const PluginCommand &pcmd) command_result Core::runCommand(color_ostream &con, const std::string &first_, vector &parts) { std::string first = first_; + CommandDepthCounter counter; + if (!counter.ok()) + { + con.printerr("Cannot invoke \"%s\": maximum command depth exceeded (%i)\n", + first.c_str(), CommandDepthCounter::MAX_DEPTH); + return CR_FAILURE; + } + command_result res; if (!first.empty()) {