hxcpp icon indicating copy to clipboard operation
hxcpp copied to clipboard

`cpp.Pointer.ofArray` breaks empty `Bytes` comparison

Open Aurel300 opened this issue 2 years ago • 1 comments

var x = haxe.io.Bytes.alloc(0);
cpp.Pointer.ofArray(x.getData());
var y = haxe.io.Bytes.ofHex("");
trace(x.compare(y));

The output is 1. If the second line is removed, the output is 0 as expected. Although x.length and y.length remain at 0, in the actual Array::Memcmp implementation, length for the first argument is 1.

Aurel300 avatar Feb 12 '23 16:02 Aurel300

Here is a more stripped down sample (adapted from #1092):

function main() {
	final array:Array<Int> = [];
	cpp.Pointer.ofArray(array);
	trace(array.length); // 1
}

cpp.Pointer.ofArray does &array[0], and the native [] operator extends the length of the array to 1. We can instead use array->Pointer() which returns the base pointer value, which is null if no array space has been allocated yet.

tobil4sk avatar Oct 19 '25 19:10 tobil4sk