Skip to content

Commit

Permalink
infinite loop detection for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Mar 4, 2021
1 parent 5cebf36 commit d37b99d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/injected/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Say get_say()
}
}

void infinite_loop(lua_State* argst, lua_Debug * argdb) {
luaL_error(argst, "Reached the script execution amount.");
};

Script::Script(std::string script, std::string file, bool enable)
{
strcpy(code, script.data());
Expand Down Expand Up @@ -711,6 +715,8 @@ bool Script::run(ImDrawList *dl)
}
try
{
lua_sethook(lua.lua_state(), NULL, 0, 0);
lua_sethook(lua.lua_state(), &infinite_loop, LUA_MASKCOUNT, 100000);
drawlist = dl;
sol::optional<std::string> meta_name = lua["meta"]["name"];
sol::optional<std::string> meta_version = lua["meta"]["version"];
Expand Down
2 changes: 2 additions & 0 deletions src/injected/script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ struct ScriptMeta
Movable *get_entity(uint32_t id);
std::tuple<float, float, int> get_position(uint32_t id);
std::string sanitize(std::string data);
void infinite_loop(lua_State *argst, lua_Debug *argdb);

using Callback = std::variant<IntervalCallback, TimeoutCallback, ScreenCallback>;

class Script
{
public:
sol::state lua;

char code[204800];
std::string result = "";
ScriptState state = {nullptr, 0, 0, 0, 0, 0, 0, 0};
Expand Down

0 comments on commit d37b99d

Please sign in to comment.