zig icon indicating copy to clipboard operation
zig copied to clipboard

Slice operations `slice_sentinel` and `slice_length` (with sentinel) produce runtime safety checks testing incorrect (underestimated) upper bounds

Open amp-59 opened this issue 1 year ago • 0 comments

Zig Version

0.13.0-dev.46+3648d7df1

Steps to Reproduce and Observed Behaviour

Compile and run example program with zig run underestimate_upper_bound.zig underestimate_upper_bound.zig:

var dest_end: usize = 3;
pub fn main() void {
    const src_mem: [3:0]usize = .{ 0, 0, 0 };
    const src_ptr: [:0]const usize = &src_mem;
    _ = src_ptr[0..dest_end :0];
    _ = src_ptr[0..][0..dest_end :0];
}

Output:

zig run underestimate_upper_bound.zig
thread 96316 panic: index out of bounds: index 4, len 3
./underestimate_upper_bound.zig:5:16: 0x1033e49 in main (underestimate_upper_bound)
    _ = src_ptr[0..dest_end :0];
               ^
~/.local/src/zig-linux-x86_64-0.13.0-dev.46+3648d7df1/lib/std/start.zig:501:22: 0x1033669 in posixCallMainAndExit (underestimate_upper_bound)
            root.main();
                     ^
~/.local/src/zig-linux-x86_64-0.13.0-dev.46+3648d7df1/lib/std/start.zig:253:5: 0x10331d1 in _start (underestimate_upper_bound)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)

Expected Behaviour

The panic condition should match the compile error condition.

amp-59 avatar Apr 28 '24 20:04 amp-59