From 778a5e35a47aae4031e9d6d5b16f667b8ab8f182 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Tue, 27 May 2025 21:39:57 +0200 Subject: [PATCH] fix(lua): stack overflow in signal --- core/src/datatypes/signal.cpp | 11 +++++++++++ core/src/objects/script.cpp | 2 ++ 2 files changed, 13 insertions(+) diff --git a/core/src/datatypes/signal.cpp b/core/src/datatypes/signal.cpp index 70522c2..5fbec19 100644 --- a/core/src/datatypes/signal.cpp +++ b/core/src/datatypes/signal.cpp @@ -2,6 +2,7 @@ #include "datatypes/base.h" #include "meta.h" #include "lua.h" +#include #include #include #include @@ -30,7 +31,15 @@ LuaSignalConnection::~LuaSignalConnection() { 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 args) { + // stackdump(state); lua_State* thread = lua_newthread(state); // Push function @@ -45,6 +54,8 @@ void LuaSignalConnection::Call(std::vector args) { Logger::error(lua_tostring(thread, -1)); lua_pop(thread, 1); // Pop return value } + + lua_pop(state, 1); // Pop thread } // diff --git a/core/src/objects/script.cpp b/core/src/objects/script.cpp index 235e67d..c19df66 100644 --- a/core/src/objects/script.cpp +++ b/core/src/objects/script.cpp @@ -117,6 +117,8 @@ int script_delay(lua_State* L) { luaL_checktype(L, 2, LUA_TFUNCTION); 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_xmove(L, Lt, 1); // move func lua_pop(L, 1); // pop secs