]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Test] Fix additional Lua 5.4 compatibility issues in fuzz tests
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 6 Dec 2025 08:47:32 +0000 (08:47 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 6 Dec 2025 08:47:32 +0000 (08:47 +0000)
- base32.lua: Add cdata_to_number helper for size_t conversion
- rfc2047.lua: Use math.floor for string.sub indices (Lua 5.4 requires integers)

test/lua/unit/base32.lua
test/lua/unit/rfc2047.lua

index e6eaf7f686017c34dbfbf260f81d772efbee0279..c49d9ff9fd212d027055780e6c954e452083aa43 100644 (file)
@@ -14,6 +14,13 @@ context("Base32 encodning", function()
     int memcmp(const void *a1, const void *a2, size_t len);
   ]]
 
+  -- Helper to convert cdata numbers to Lua numbers (cffi-lua compatibility)
+  local function cdata_to_number(v)
+    local n = tonumber(v)
+    if n then return n end
+    return tonumber(tostring(v):match("^(%d+)"))
+  end
+
   local function random_buf(max_size)
     local l = ffi.C.ottery_rand_unsigned() % max_size + 1
     local buf = ffi.new("unsigned char[?]", l)
@@ -47,7 +54,7 @@ context("Base32 encodning", function()
         local nl = ffi.new("size_t [1]")
         local nb = ffi.C.rspamd_decode_base32(bs, #bs, nl, how)
 
-        local reported_len = tonumber(nl[0]) or 0
+        local reported_len = cdata_to_number(nl[0]) or 0
         assert_equal(reported_len, l,
             string.format("invalid size reported: %s reported vs %s expected", tostring(reported_len), tostring(l)))
         local cmp = ffi.C.memcmp(b, nb, l)
index 7f341fa363b91e548728505a943797f6a7a48045..b65b67b5e29d0387a6c9376d1a22c3d17dc87313 100644 (file)
@@ -72,8 +72,9 @@ context("RFC2047 decoding", function()
       for _ = 0,1000 do
         local r1 = math.random()
         local r2 = math.random()
-        local sl1 = #str / 2.0 * r1
-        local sl2 = #str / 2.0 * r2
+        -- Use math.floor for Lua 5.4 compatibility (string.sub requires integers)
+        local sl1 = math.floor(#str / 2.0 * r1)
+        local sl2 = math.floor(#str / 2.0 * r2)
 
         local s1 = tostring(util.encode_base64(string.sub(str, 1, sl1)))
         local s2 = tostring(util.encode_base64(string.sub(str, sl1 + 1, sl2)))