Re: Segfault when initializing VLA cdata

  • From: Mike Pall <mike-1503@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 10 Mar 2015 12:58:16 +0100

Cheyi Lin wrote:
> > 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.
> 
> a) why it crashes only with a Lua string which the length is multiple of 8
> bytes?

Because the byte that's overwritten by the zero terminator is not
unused in this case.

> 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)'?

A VLA is NOT considered as having a fixed size, so the copy does
NOT stop early. It copies #s+1 bytes, including the zero
terminator, which writes beyond the end of the array.

--Mike

Other related posts: