lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Florian Weimer wrote:
> This:
> 
> | local ffi = require "ffi"
> | print(string.byte(ffi.string(ffi.new("char[3]", "ABC"), 3), 1, -1))
> 
> prints:
> 
> | 65      66      0
> 
> I think this is rather surprising.  I would have expected:
> 
> | 65      66      67
> 
> Is this a bug or a feature?

Well, it was a feature. But I guess it was a bit over-protective.

I've changed it now to match the behavior of ffi.copy() and to the
overall no-handholding policy of the FFI: if the size of the
destination array is smaller than #str+1, then the result is not
zero-terminated. If you really need a zero-terminated array, then
provide one that is big enough, or explicitly store the zero or
use ffi.copy() with a length argument.

--Mike