fix(lua): stack overflow in signal

This commit is contained in:
maelstrom 2025-05-27 21:39:57 +02:00
parent 18b12ea1ad
commit 778a5e35a4
2 changed files with 13 additions and 0 deletions

View file

@ -2,6 +2,7 @@
#include "datatypes/base.h" #include "datatypes/base.h"
#include "meta.h" #include "meta.h"
#include "lua.h" #include "lua.h"
#include <luajit-2.1/lua.h>
#include <pugixml.hpp> #include <pugixml.hpp>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -30,7 +31,15 @@ LuaSignalConnection::~LuaSignalConnection() {
luaL_unref(state, LUA_REGISTRYINDEX, function); luaL_unref(state, LUA_REGISTRYINDEX, function);
} }
static void stackdump(lua_State* L) {
for (int i = lua_gettop(L); i >= 1; i--) {
printf("Obj: %s\n", lua_typename(L, lua_type(L, i)));
}
printf("\n\n");
}
void LuaSignalConnection::Call(std::vector<Data::Variant> args) { void LuaSignalConnection::Call(std::vector<Data::Variant> args) {
// stackdump(state);
lua_State* thread = lua_newthread(state); lua_State* thread = lua_newthread(state);
// Push function // Push function
@ -45,6 +54,8 @@ void LuaSignalConnection::Call(std::vector<Data::Variant> args) {
Logger::error(lua_tostring(thread, -1)); Logger::error(lua_tostring(thread, -1));
lua_pop(thread, 1); // Pop return value lua_pop(thread, 1); // Pop return value
} }
lua_pop(state, 1); // Pop thread
} }
// //

View file

@ -117,6 +117,8 @@ int script_delay(lua_State* L) {
luaL_checktype(L, 2, LUA_TFUNCTION); luaL_checktype(L, 2, LUA_TFUNCTION);
lua_State* Lt = lua_newthread(L); // Create a new thread lua_State* Lt = lua_newthread(L); // Create a new thread
// I think this is memory abuse??
// Wouldn't popping the thread in this case make it eligible for garbage collection?
lua_pop(L, 1); // pop the newly created thread so that xmove moves func instead of it into itself lua_pop(L, 1); // pop the newly created thread so that xmove moves func instead of it into itself
lua_xmove(L, Lt, 1); // move func lua_xmove(L, Lt, 1); // move func
lua_pop(L, 1); // pop secs lua_pop(L, 1); // pop secs