[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: LuaJIT FFI: char[?] initialization from string
- From: Mike Pall <mikelu-1101@...>
- Date: Mon, 24 Jan 2011 15:59:06 +0100
Florian Weimer wrote:
> | local ffi = require "ffi"
> | print(string.byte(ffi.string(ffi.new("char", "ABC"), 3), 1, -1))
> | 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.