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

Slice operation produces runtime safety check with impossible success condition #19796

Open
amp-59 opened this issue Apr 28, 2024 · 0 comments · May be fixed by #19764
Open

Slice operation produces runtime safety check with impossible success condition #19796

amp-59 opened this issue Apr 28, 2024 · 0 comments · May be fixed by #19764

Comments

@amp-59
Copy link
Contributor

amp-59 commented Apr 28, 2024

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.

@amp-59 amp-59 linked a pull request Apr 28, 2024 that will close this issue
39 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant