Skip to content

Commit

Permalink
prevent recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
void-inject committed Jan 9, 2025
1 parent bec0cc1 commit cacbe78
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions library/src/interactor_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,14 @@ interactor& interactor_impl::initCommands()
this->addCommand("alias",
[&](const std::vector<std::string>& args)
{
if (args.size() < 2)
{
throw interactor_impl::invalid_args_exception("alias command requires at least 2 arguments")
}

// Validate the alias arguments
check_args(args, 2, "alias");
const std::string& aliasName = args[0];
// combine set of command to aliasCommand
// Combine all remaining arguments into the alias command
std::string aliasCommand;
for (size_t i = 1; i < args.size(); ++i)
{
Expand All @@ -782,6 +786,12 @@ interactor& interactor_impl::initCommands()
aliasCommand += args[i];
}

// Prevent recursion
if (aliasName == aliasCommand)
{
throw interactor_impl::invalid_args_exception("Alias cannot reference itself: " + aliasName);
}

// Add alias to the map
AliasMap[aliasName] = aliasCommand;
log::info("Alias added: ", aliasName, "", aliasCommand);
Expand Down Expand Up @@ -848,8 +858,8 @@ bool interactor_impl::triggerCommand(std::string_view command)
if (aliasIt != AliasMap.end())
{
std::vector<std::string> aliasTokens = utils::tokenize(aliasIt->second);
tokens.erase(tokens.begin());
tokens.insert(tokens.begin(), aliasTokens.begin(), aliasTokens.end());
tokens.erase(tokens.begin() + aliasTokens.size());
action = tokens[0];
}

Expand Down

0 comments on commit cacbe78

Please sign in to comment.