Skip to content
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

Closed
perillo opened this issue May 7, 2022 · 7 comments
Closed

read, etc. should check slice.len == 0 #11604

perillo opened this issue May 7, 2022 · 7 comments
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@perillo
Copy link
Contributor

perillo commented May 7, 2022

Zig Version

0.10.0-dev.2103+ac1aaec9c

Steps to Reproduce

Consider this program:

const std = @import("std");
const debug = std.debug;

pub fn main() void {
    var array: [0]u8 = [_]u8{};
    var slice: []u8 = &array;

    const ptr = @ptrToInt(slice.ptr);
    debug.print("ptr: 0x{x}\n", .{ptr});
}

A problem with the current behavior is that passing a zero length buffer to read will crash the program (close returns EFAULT):

const std = @import("std");
const io = std.io;

pub fn main() !void {
    const stdin = io.getStdIn();

    var buf: [0]u8 = [_]u8{};
    _ = try stdin.read(&buf);
}

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:

ptr: 0

Actual Behavior

The output is:

ptr: 0xaaaaaaaaaaaaaaaa

that is, undefined.

@perillo perillo added the bug Observed behavior contradicts documented or intended behavior label May 7, 2022
@Vexu
Copy link
Member

Vexu commented May 9, 2022

Why do you expect the pointer to be 0? That is not even a value allowed by the type.

@perillo
Copy link
Contributor Author

perillo commented May 10, 2022

@Vexu, indeed it does not need to be 0. In order for the read example to work, it just needs to be a valid pointer.

Thanks.

@leecannon
Copy link
Contributor

But what should it be a pointer too?
[0]u8 is zero size so doesn't exist in memory at runtime, so you can't get its address.

@perillo
Copy link
Contributor Author

perillo commented May 11, 2022

@leecannon, I think #6706 should solve this problem.

Current behavior does not feels right, to me. You can not get the address of [0]u, but you can coerce it to a slice, whose .ptr field holds an undefined pointer.

@perillo perillo changed the title @ptrToInt should return 0 from a zero size slice ptr @ptrToInt should return a valid pointer from a zero size slice ptr May 11, 2022
@perillo
Copy link
Contributor Author

perillo commented May 11, 2022

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.

@Vexu
Copy link
Member

Vexu commented May 17, 2022

@Vexu, indeed it does not need to be 0. In order for the read example to work, it just needs to be a valid pointer.

Sounds like the actual bug is read not checking whether the slice has a non zero length before doing any operations on the pointer.

It seems that there is another "unexpected" behavior with empty slices:

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.

@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Jul 29, 2022
@Vexu Vexu changed the title @ptrToInt should return a valid pointer from a zero size slice ptr read, etc. should check slice.len == 0 Jul 29, 2022
@Vexu Vexu added this to the 0.12.0 milestone Jul 29, 2022
@perillo
Copy link
Contributor Author

perillo commented Dec 13, 2022

I think this issue can be closed after commit d0eef26.

@Vexu Vexu closed this as completed Dec 13, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

4 participants