-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
read
, etc. should check slice.len == 0
#11604
Comments
Why do you expect the pointer to be |
@Vexu, indeed it does not need to be Thanks. |
But what should it be a pointer too? |
@leecannon, I think #6706 should solve this problem. Current behavior does not feels right, to me. You can not get the address of |
ptr
ptr
It seems that there is another "unexpected" behavior with empty slices: test "empty slice ptr from zero bit array" {
const a: [0]u8 = [_]u8{};
var b: []u8 = &a;
debug.print("len b: {}\n", .{b.len});
} test "empty slice ptr" {
const a: []u8 = try testing.allocator.alloc(u8, 10);
defer testing.allocator.free(a);
var b: []u8 = a[0..0];
debug.print("len b: {}\n", .{b.len});
try testing.expectEqual(a.ptr, b.ptr);
} Both are empty slices, but the latter has a valid pointer. Code adapted from #4771. Thanks. |
Sounds like the actual bug is
I would find it more unexpected for slicing to turn an invalid pointer into a valid one or the other way around. The key property for both of these slices is the length being zero. |
ptr
read
, etc. should check slice.len == 0
I think this issue can be closed after commit d0eef26. |
Zig Version
0.10.0-dev.2103+ac1aaec9c
Steps to Reproduce
Consider this program:
A problem with the current behavior is that passing a zero length buffer to
read
will crash the program (close
returnsEFAULT
):This C program works as expected: https://glot.io/snippets/g9k7ypryux.
This Go program also works as expected: https://go.dev/play/p/5LcVU6It_xZ
Expected Behavior
The output to be:
Actual Behavior
The output is:
that is, undefined.
The text was updated successfully, but these errors were encountered: