cffi-lua icon indicating copy to clipboard operation
cffi-lua copied to clipboard

LuaJIT FFI versus CFFI: CFFI permits setting a new function to a callback on which free() has been called, causing a crash upon execution of the callback or free()

Open gynt opened this issue 4 months ago • 2 comments

cffi = require("cffi")
callback = cffi.cast("void (*)()", function() end)
callback:free()
callback:set(function() end)
callback:free() -- this line crashes

gynt avatar Aug 23 '25 09:08 gynt

Title is kind of misleading. LuaJIT does not support this either:

ffi = require 'ffi'
callback = ffi.cast("void (*)()", function() end)
callback:free()
callback:set(function() end) -- error: bad callback

Probably should replicate this behavior, and throw a Lua error instead of outright crashing.

rweichler avatar Aug 23 '25 17:08 rweichler

Good point. To make it in line with LuaJIT, it should fail on using set on a freed callback. Although the code is wrong per the documentation, being able to set a free'd callback is confusing.

gynt avatar Aug 23 '25 20:08 gynt