ffi.string(nil) seg faults

  • From: Basurita <basuraparabarspi@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 06 Jan 2013 02:43:50 -0200

As the subject says.
When trying to convert a cdata char*, which in fact is NULL (cdata<const char *>: NULL) to a Lua string using ffi.string(), the program aborts with segmentation fault.
I know, I should check first, bla bla (see below), but maybe a catchable Lua 
error would be more gracious. :-)

Related to this, cdata objects which happen to be NULL pointers, have a 
half-assed relationship to Lua nil.

For example:

a= some_c_data_which_is_NULL_pointer
print(a) -- prints cdata<somectype>: NULL
print(a == nil) -- it's true!! kind of useful…

-- and now, a very common Lua idiom, since a == nil...
print(a or "hello") -- guess what, it prints cdata<somectype>: NULL
print(ffi.string(a or "hello")) -- WTF! segmentation fault!!1!!eleven1!
print(ffi.string(a ~= nil a or "hello")) -- this one works, but unnecessarily 
verbose


BBB

Other related posts: