]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Handle Lua 5.4 require returning two values
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 6 Dec 2025 14:18:20 +0000 (14:18 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 6 Dec 2025 14:18:20 +0000 (14:18 +0000)
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.

src/libstat/stat_config.c
src/lua/lua_config.c
src/plugins/fuzzy_check.c
src/rspamadm/rspamadm.c

index 5ada7d468193e2b10b7db189742ce9969a058a63..ecc96c1741d0e42e798d326e428d6dfc647067fc 100644 (file)
@@ -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",
index c7534980a8483ea5890064efb97919e8075f3564..b863e11d728e6e48e7fa9928709ef8f79af45e7a 100644 (file)
@@ -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",
index 030d14a7b3dc673f8da50881b1684869d6540097..75da5640da9b0785595566b4760952366a6bc594 100644 (file)
@@ -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",
index 655b8606d0b50619e283d424f93063931df36faf..e3b95895c7377a0c27678886f9cd707bc973be05 100644 (file)
@@ -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);