]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Test] Fix FFI string-to-pointer conversions for cffi-lua
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 5 Dec 2025 17:22:22 +0000 (17:22 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 5 Dec 2025 17:22:22 +0000 (17:22 +0000)
cffi-lua is stricter than LuaJIT FFI and requires explicit conversion
of Lua strings to C pointers. Use ffi.new/ffi.copy for the conversion.

test/lua/unit/base32.lua
test/lua/unit/base64.lua

index 550c1cfb864a1d6e1b9b66e7c5cb2671e87bfc78..0996f4efd03a5444a1cd2139d2c9486f7ce7476e 100644 (file)
@@ -29,7 +29,8 @@ context("Base32 encodning", function()
     }
 
     for _,c in ipairs(cases) do
-      local b = ffi.C.rspamd_encode_base32(c[1], #c[1], 0)
+      local inp = ffi.new("unsigned char[?]", #c[1], c[1])
+      local b = ffi.C.rspamd_encode_base32(inp, #c[1], 0)
       local s = ffi.string(b)
       ffi.C.g_free(b)
       assert_equal(s, c[2], s .. " not equal " .. c[2])
index a48c23ce241f745c5c2af3d5e22e6488603089a0..94dadc0ce6e11597cc1b4e29615106a33e185352 100644 (file)
@@ -54,7 +54,9 @@ context("Base64 encoding", function()
 
     local nl = ffi.new("size_t [1]")
     for _,c in ipairs(cases) do
-      local b = ffi.C.rspamd_encode_base64(c[1], #c[1], 0, nl)
+      local inp = ffi.new("unsigned char[?]", #c[1] + 1)
+      ffi.copy(inp, c[1])
+      local b = ffi.C.rspamd_encode_base64(inp, #c[1], 0, nl)
       local s = ffi.string(b)
       ffi.C.g_free(b)
       assert_equal(s, c[2], s .. " not equal " .. c[2])
@@ -86,7 +88,9 @@ in the continued and indefatigable generation of knowledge, exceeds the short
 vehemence of any carnal pleasure.]]
     local b64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbQpvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodAppbiB0aGUgY29udGlu\r\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\nZSBzaG9ydAp2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
     local nl = ffi.new("size_t [1]")
-    local b = ffi.C.rspamd_encode_base64(text, #text, 76, nl)
+    local inp = ffi.new("unsigned char[?]", #text + 1)
+    ffi.copy(inp, text)
+    local b = ffi.C.rspamd_encode_base64(inp, #text, 76, nl)
     local cmp = ffi.C.memcmp(b, b64, nl[0])
     ffi.C.g_free(b)
     assert_equal(cmp, 0)