const std = @import("std");
test {
var b: []u8 = undefined;
b.ptr = @ptrFromInt(20);
b.len = 0;
const r = std.mem.alignInSlice(b, 4);
try std.testing.expect(r != null and r.?.len == 0);
}
because r is null.
IIUC this happens because std.mem.alignInSlice calls std.mem.sliceAsBytes, which replaces empty slices with &[0]u8{}.
Expected Behavior
I would expect either std.mem.alignInSlice to support passing in empty slices, or for there to be a doc comment about this. I think that if an empty slice with a pointer that is aligned to the boundary is passed in, then an empty slice should be returned. Note that in the example above, if b.ptr == @ptrFromInt(19) and b.len == 1, then an empty slice is returned. However, I'm not sure I fully understand how Zig wants to treat the .ptr of a slice when .len == 0, so maybe the status quo is OK: I would appreciate if someone could shed some light on this.
To me, it also seems like a potential issue/footgun that sliceAsBytes can return a slice that's not at the same location as the input slice if the input slice is empty, but again maybe this is OK.
At the very least, std.mem.alignInSlice should have a doc comment about passing in empty slices, because the current behavior is confusing and can lead to bugs in code that might naively use the function.
Zig Version
0.14.0-dev.2271+f845fa04a
Steps to Reproduce and Observed Behavior
The following test fails
because
r
isnull
.IIUC this happens because
std.mem.alignInSlice
callsstd.mem.sliceAsBytes
, which replaces empty slices with&[0]u8{}
.Expected Behavior
I would expect either
std.mem.alignInSlice
to support passing in empty slices, or for there to be a doc comment about this. I think that if an empty slice with a pointer that is aligned to the boundary is passed in, then an empty slice should be returned. Note that in the example above, ifb.ptr == @ptrFromInt(19)
andb.len == 1
, then an empty slice is returned. However, I'm not sure I fully understand how Zig wants to treat the.ptr
of a slice when.len == 0
, so maybe the status quo is OK: I would appreciate if someone could shed some light on this. To me, it also seems like a potential issue/footgun thatsliceAsBytes
can return a slice that's not at the same location as the input slice if the input slice is empty, but again maybe this is OK. At the very least,std.mem.alignInSlice
should have a doc comment about passing in empty slices, because the current behavior is confusing and can lead to bugs in code that might naively use the function.