Segfault when initializing VLA cdata

  • From: Cheyi Lin <cheyi.lin@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 10 Mar 2015 19:32:57 +0800

​Hi,

I got segfault when initializing a VLA cdata with a Lua string which the
length is multiple of 8 bytes

env: git v2.1 HEAD, Linux x86_64

-- crashes
local ffi = require("ffi")
local s = "12345678"

for i = 1, 100000 do
    local r = ffi.new("uint8_t[?]", #s, s)
end

The manual http://luajit.org/ext_ffi_semantics.html#init says:

> Byte arrays may also be initialized with a Lua string.
> This copies the whole string plus a terminating zero-byte.
> The copy stops early only if the array has a known, fixed size.

So this issue could be solved through this way:

-- works
local r = ffi.new("uint8_t[?]", #s + 1, s)

or copy the Lua string manually:

-- works too
local r = ffi.new("uint8_t[?]", #s)
ffi.copy(r, s, #s)

Just wondering:
a) why it crashes only with a Lua string which the length is multiple of 8
bytes?
b) why 'local r = ffi.new("uint8_t[?]", #s, s)' is not equivalent to 'local
r = ffi.new("uint8_t[?]", #s); ffi.copy(r, s, #s)'?


Regards,
Cheyi​

Other related posts: