Re: luajit string to char * conversion

  • From: Wolfgang Pupp <wolfgang.pupp@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 27 Nov 2014 12:03:14 +0100

Note that you *must* compare with nil for NULL-pointer checks.
If you coerce cdata to boolean type (e.g. by if-condition), you always
get <true>. Example:

local v = ffi.new'char *'

if v == nil then
  print "this is working as intended"
end

-- this probably doesn't do what you think it does.
-- Even NULL-pointer cdata (which compares equal to nil, see above),
-- satisfies an if-condition (-> surprise segfault in this case):
if v then
  print "surprise segfault!"
  nullptr[0] = 0
end

--Wolfgang

Other related posts: