q66 / cffi-lua

A portable C FFI for Lua 5.1+
MIT License
176 stars 24 forks source link

Fix global array variables and ffi.string() for flexible arrays #21

Closed vsergeev closed 3 years ago

vsergeev commented 3 years ago

fix global array variables

the cdata object created for global arrays was dereferencing the library symbol instead of treating it as a pointer. this causes a segfault when the array is accessed by the user and dereferenced a second time.

I made this modification to the to_lua() code path, which is called from get_global(), but I'm not sure if code path is used in other contexts where a builtin array should be handled differently.

fix ffi.string() for flexible arrays

ffi_module::string_f() limits serialization of the C_BUILTIN_ARRAY to decl.alloc_size() length, which is 0 for flexible arrays and results in the empty string. this commit changes flexible arrays to be treated as null-terminated char pointers.

q66 commented 3 years ago

the global array fix has incorrect semantics here, as any context where an array is returned should make a C++-style reference; __newindex for cdata already handles this specially, but there is in fact no need for any special handling, so I'll just fix it

q66 commented 3 years ago

Fixed 2884f11a1d07edeafc8e12c0deac55dd5904e5d2 , 2e8c378d33270ec647ad57f2b6067c6f2c74a29c