zig
zig copied to clipboard
Slice operation produces runtime safety check with impossible success condition
Zig Version
0.13.0-dev.46+3648d7df1
Steps to Reproduce and Observed Behaviour
Compile and run example program with zig run slice_start_sentinel_always_out_of_bounds.zig
slice_start_sentinel_always_out_of_bounds.zig:
pub fn main() !void {
var buf: *const [8]u8 = &([1]u8{0} ** 8);
_ = buf[0.. :1];
}
Output:
zig run slice_start_sentinel_always_out_of_bounds.zig
thread 96688 panic: index out of bounds: index 9, len 8
./slice_start_sentinel_always_out_of_bounds.zig:3:12: 0x10334fd in main (slice_start_sentinel_always_out_of_bounds)
_ = buf[0.. :1];
^
~/.local/src/zig-linux-x86_64-0.13.0-dev.46+3648d7df1/lib/std/start.zig:511:37: 0x1033494 in posixCallMainAndExit (slice_start_sentinel_always_out_of_bounds)
const result = root.main() catch |err| {
^
~/.local/src/zig-linux-x86_64-0.13.0-dev.46+3648d7df1/lib/std/start.zig:253:5: 0x1032fd1 in _start (slice_start_sentinel_always_out_of_bounds)
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
Expected Behaviour
For this syntax the end index is equal to the length and the sentinel index is always one greater than the end index. Therefore the sentinel index is always greater than the length.
This variant (slice_start with sentinel and pointer without sentinel) should produce a compile error, noting that the sentinel index is always out-of-bounds.