Skip to content

Commit

Permalink
Fix crash when calling addEvent (#3361)
Browse files Browse the repository at this point in the history
* Small optimization: parameters is now a vector, simple because there was no reason to use std::list here + we call reserve now
When addEvent is called, check if there is enough parameters(2), if first parameter is a function(callback) and if second parameter is a number(delay)

Co-authored-by: Ranieri Althoff <1993083+ranisalt@users.noreply.github.com>
  • Loading branch information
yamaken93 and ranisalt committed Mar 10, 2021
1 parent e333410 commit 2b8bc10
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/luascript.cpp
Expand Up @@ -3589,12 +3589,25 @@ int LuaScriptInterface::luaAddEvent(lua_State* L)
}

int parameters = lua_gettop(globalState);
if (!isFunction(globalState, -parameters)) { //-parameters means the first parameter from left to right

if (parameters < 2) {
reportErrorFunc(fmt::format("Not enough parameters: {:d}.", parameters));
pushBoolean(L, false);
return 1;
}

if (!isFunction(globalState, 1)) {
reportErrorFunc("callback parameter should be a function.");
pushBoolean(L, false);
return 1;
}

if (!isNumber(globalState, 2)) {
reportErrorFunc("delay parameter should be a number.");
pushBoolean(L, false);
return 1;
}

if (g_config.getBoolean(ConfigManager::WARN_UNSAFE_SCRIPTS) || g_config.getBoolean(ConfigManager::CONVERT_UNSAFE_SCRIPTS)) {
std::vector<std::pair<int32_t, LuaDataType>> indexes;
for (int i = 3; i <= parameters; ++i) {
Expand Down Expand Up @@ -3670,7 +3683,8 @@ int LuaScriptInterface::luaAddEvent(lua_State* L)
}

LuaTimerEventDesc eventDesc;
for (int i = 0; i < parameters - 2; ++i) { //-2 because addEvent needs at least two parameters
eventDesc.parameters.reserve(parameters - 2); // safe to use -2 since we garanteed that there is at least two parameters
for (int i = 0; i < parameters - 2; ++i) {
eventDesc.parameters.push_back(luaL_ref(globalState, LUA_REGISTRYINDEX));
}

Expand Down
2 changes: 1 addition & 1 deletion src/luascript.h
Expand Up @@ -85,7 +85,7 @@ struct LuaVariant {
struct LuaTimerEventDesc {
int32_t scriptId = -1;
int32_t function = -1;
std::list<int32_t> parameters;
std::vector<int32_t> parameters;
uint32_t eventId = 0;

LuaTimerEventDesc() = default;
Expand Down

0 comments on commit 2b8bc10

Please sign in to comment.