From: Vsevolod Stakhov Date: Sat, 6 Dec 2025 14:18:20 +0000 (+0000) Subject: [Fix] Handle Lua 5.4 require returning two values X-Git-Tag: 3.14.2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b62d047fa2bfef42792fc4e7d75f8f31a6174a4;p=thirdparty%2Frspamd.git [Fix] Handle Lua 5.4 require returning two values Lua 5.4's require() returns both the module and the file path, while LuaJIT returns only the module. Save stack top before luaL_dostring and restore to top+1 after to keep only the first return value. --- diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c index 5ada7d4681..ecc96c1741 100644 --- a/src/libstat/stat_config.c +++ b/src/libstat/stat_config.c @@ -180,14 +180,14 @@ void rspamd_stat_init(struct rspamd_config *cfg, struct ev_loop *ev_base) stat_ctx->lua_stat_tokens_ref = -1; /* Interact with lua_stat */ + int lua_stat_top = lua_gettop(L); if (luaL_dostring(L, "return require \"lua_stat\"") != 0) { msg_err_config("cannot require lua_stat: %s", lua_tostring(L, -1)); } else { -#if LUA_VERSION_NUM >= 504 - lua_settop(L, -2); -#endif + /* Lua 5.4's require returns 2 values (module + path), keep only first */ + lua_settop(L, lua_stat_top + 1); if (lua_type(L, -1) != LUA_TTABLE) { msg_err_config("lua stat must return " "table and not %s", diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index c7534980a8..b863e11d72 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -5115,11 +5115,14 @@ lua_config_register_re_selector(lua_State *L) } } + int selector_top = lua_gettop(L); if (luaL_dostring(L, "return require \"lua_selectors\"") != 0) { msg_warn_config("cannot require lua_selectors: %s", lua_tostring(L, -1)); } else { + /* Lua 5.4's require returns 2 values (module + path), keep only first */ + lua_settop(L, selector_top + 1); if (lua_type(L, -1) != LUA_TTABLE) { msg_warn_config("lua selectors must return " "table and not %s", @@ -5541,11 +5544,14 @@ lua_config_register_re_selector_scoped(lua_State *L) } } + int selector_top = lua_gettop(L); if (luaL_dostring(L, "return require \"lua_selectors\"") != 0) { msg_warn_config("cannot require lua_selectors: %s", lua_tostring(L, -1)); } else { + /* Lua 5.4's require returns 2 values (module + path), keep only first */ + lua_settop(L, selector_top + 1); if (lua_type(L, -1) != LUA_TTABLE) { msg_warn_config("lua selectors must return " "table and not %s", diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index 030d14a7b3..75da5640da 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -2609,15 +2609,15 @@ int fuzzy_check_module_config(struct rspamd_config *cfg, bool validate) fuzzy_module_ctx->cleanup_rules_ref = -1; /* Interact with lua_fuzzy */ + int lua_fuzzy_top = lua_gettop(L); if (luaL_dostring(L, "return require \"lua_fuzzy\"") != 0) { msg_err_config("cannot require lua_fuzzy: %s", lua_tostring(L, -1)); fuzzy_module_ctx->enabled = FALSE; } else { -#if LUA_VERSION_NUM >= 504 - lua_settop(L, -2); -#endif + /* Lua 5.4's require returns 2 values (module + path), keep only first */ + lua_settop(L, lua_fuzzy_top + 1); if (lua_type(L, -1) != LUA_TTABLE) { msg_err_config("lua fuzzy must return " "table and not %s", diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c index 655b8606d0..e3b95895c7 100644 --- a/src/rspamadm/rspamadm.c +++ b/src/rspamadm/rspamadm.c @@ -248,12 +248,17 @@ rspamadm_execute_lua_ucl_subr(int argc, char **argv, script_name); } + int top = lua_gettop(L); + if (luaL_dostring(L, str) != 0) { msg_err("cannot execute lua script %s: %s", str, lua_tostring(L, -1)); return FALSE; } else { + /* Lua 5.4's require returns 2 values (module + path), keep only first */ + lua_settop(L, top + 1); + if (lua_type(L, -1) == LUA_TTABLE) { lua_pushstring(L, "handler"); lua_gettable(L, -2);