ffi initializer bit me today

  • From: Cosmin Apreutesei <cosmin.apreutesei@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sat, 8 Sep 2012 18:22:24 +0300

Hi,

Just wanted to mention a ffi (documented) behavior that can lead to
subtle bugs IMHO:

local cdata = ffi.new('unsigned char[?]', 2, 'ab')

This copies 3 bytes.

local cdata = ffi.new('unsigned char[?]', 2, {'a','b','c'})

This copies 3 bytes.

local cdata = ffi.new('unsigned char[2]', 'abcdef')

This copies 2 bytes.

local cdata = ffi.new('unsigned char[2]', {'a','b','c'})

This results in error (too many initializers).


To sum up, no bounds checking for VLAs, silent trimming for string
initializers on FSAs, bounds checking for table initializers or FSAs.

The VLA case was the one that bit me today. It would be cool to add
bounds check to that too.

Other related posts: