-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
sema: add compile error for OOB by-length slice of array #18410
Conversation
could you add a test case for the double runtime case? i.e test {
var buf: [5]u8 = undefined;
const slice = &buf;
var a: u32 = 6;
_ = &a;
_ = buf[a..][0..a];
_ = slice[a..][0..a];
} |
I'm not sure about the workaround I've added in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a small fix suggestion, otherwise looks good from me.
83c0bee
to
5af5d9f
Compare
5af5d9f
to
41d5aa1
Compare
Done. |
Reverted in bd46410. |
Would you rather a runtime safety check? I don't think it will be possible to have a compile error to prevent out-of-bounds length slicing without either the kind of workaround that was introduced in |
Yes, it should emit a compile error if illegal behavior is detected at compile-time. There must be no false positives (the problem of this implementation). If it cannot be detected at compile-time correctly then it should be detected at runtime. |
The analogous compile error for slicing an array normally (i.e. not 'by-length') has the same kind of false-positive, i.e. this test: test {
const array = [_]u8{ 0, 1, 2, 3, 4, 5 };
var start: usize = 0;
_ = &start;
const end = array.len + 1;
if (start + end < start + array.len) {
_ = array[start..end];
}
} results in
despite never attempting an out-of-bounds access. Should this compile error also be moved to a runtime check? |
…e-bug" This reverts commit d9d840a, reversing changes made to a04d433. This is not an adequate implementation of the missing safety check, as evidenced by the changes to std.json that are reverted in this commit. Reopens ziglang#18382 Closes ziglang#18510
…e-bug" This reverts commit d9d840a, reversing changes made to a04d433. This is not an adequate implementation of the missing safety check, as evidenced by the changes to std.json that are reverted in this commit. Reopens ziglang#18382 Closes ziglang#18510
Fixes #18382.
This change introduces a compile error when a by-length (i.e.
buf[a..][0..len]
) slice of an array with length comptime-known to be less thanlen
is performed. Previously this case would compile and not even insert a runtime safety check in safe build modes.An example of the new compile error is:
which is generated by this test: