From: Vsevolod Stakhov Date: Sat, 6 Dec 2025 11:09:03 +0000 (+0000) Subject: [Fix] Improve loadstring error handling for Lua 5.4 compatibility X-Git-Tag: 3.14.2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=738edf0cb72b43e37671bd37537bfc1065d0058c;p=thirdparty%2Frspamd.git [Fix] Improve loadstring error handling for Lua 5.4 compatibility Ensure loadstring results are checked for nil (syntax errors) before passing to pcall. This prevents errors when running with Lua 5.4 compatibility where load behavior differs slightly or when handling invalid Lua chunks. --- diff --git a/lualib/lua_dkim_tools.lua b/lualib/lua_dkim_tools.lua index 1942f4c2d8..be446f8bc0 100644 --- a/lualib/lua_dkim_tools.lua +++ b/lualib/lua_dkim_tools.lua @@ -385,7 +385,15 @@ local function prepare_dkim_signing(N, task, settings) if type(settings.use_domain_custom) == 'string' then -- Load custom function local loadstring = loadstring or load - local ret, res_or_err = pcall(loadstring(settings.use_domain_custom)) + local chunk, err = loadstring(settings.use_domain_custom) + local ret, res_or_err + if chunk then + ret, res_or_err = pcall(chunk) + else + ret = false + res_or_err = err + end + if ret then if type(res_or_err) == 'function' then settings.use_domain_custom = res_or_err diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 091bcc8ceb..06571e638d 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -1529,7 +1529,12 @@ exports.callback_from_string = function(s) inp = 'return function(...)\n' .. s .. '; end' end - local ret, res_or_err = pcall(loadstring(inp)) + local chunk, err = loadstring(inp) + if not chunk then + return false, err + end + + local ret, res_or_err = pcall(chunk) if not ret or type(res_or_err) ~= 'function' then return false, res_or_err diff --git a/src/plugins/lua/clickhouse.lua b/src/plugins/lua/clickhouse.lua index 16a8ad4ec1..2811206b90 100644 --- a/src/plugins/lua/clickhouse.lua +++ b/src/plugins/lua/clickhouse.lua @@ -1364,7 +1364,14 @@ if opts then if rule.schema and rule.first_row and rule.get_row then local first_row, get_row local loadstring = loadstring or load - local ret, res_or_err = pcall(loadstring(rule.first_row)) + local ret, res_or_err + local chunk, err = loadstring(rule.first_row) + if chunk then + ret, res_or_err = pcall(chunk) + else + ret = false + res_or_err = err + end if not ret or type(res_or_err) ~= 'function' then rspamd_logger.errx(rspamd_config, 'invalid first_row (%s) - must be a function', @@ -1373,7 +1380,13 @@ if opts then first_row = res_or_err end - ret, res_or_err = pcall(loadstring(rule.get_row)) + chunk, err = loadstring(rule.get_row) + if chunk then + ret, res_or_err = pcall(chunk) + else + ret = false + res_or_err = err + end if not ret or type(res_or_err) ~= 'function' then rspamd_logger.errx(rspamd_config,