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
runtime: consider making "slice bounds out of range" messages more explicit #29435
Comments
The slice indexes must be within the length of the slice, not its capacity. Please read the spec more carefully: https://golang.org/ref/spec#Slice_expressions |
For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. For slices, the upper index bound is the slice capacity cap(a) rather than the length. |
Note that it says "upper index bound". You can do:
That is, you can use a slice expression to extend length up to capacity. But arbitrary slice expressions outside of the length range aren't allowed. Perhaps the spec could be made clearer, but I think it's clear enough as it is. I'll leave that to @griesemer. |
low<=high
and
does't |
I have an impression that the
|
Then one section in my article should be wrong. And one SSA optimization in |
Ah, sorry, it is misunderstanding. The compiler is right.
|
But my former impression for the slice rule was really wrong. |
Are you sure? I think omitting the high index just means the length. The spec mentions it:
I'm not sure what to think of Go 1.7 changing the behavior of this code. Perhaps earlier Go versions behaved correctly and have since been broken. Or perhaps they misbehaved and have been fixed. Reading the spec again, now I think I agree with @soslanco's reading of it. My initial replies were muscle memory, as I think I've encountered very similar panics in the past. |
Ah, |
Yep! |
Perhaps the runtime panic message could be a bit more explicit. In particular, the issue here is that we're slicing Aside from that, I don't see what else there is to do in Go. The program is wrong, and the runtime is right to panic. I'll leave the issue open for now to see if anyone has any opinions on improving the runtime panic messages. |
I don't think we should change the runtime panic messages, for two reasons: (1) I'm sure that people are relying on the text of the existing messages in their programs. They shouldn't be, but they are, and it's not worth breaking them. (2) Adding additional information (such as the bounds involved) has a cost in binary size and execution. With the current ABI, passing bounds to the runtime panic routines requires stack space, which might prevent elimination of the function prolog, which can have a substantive performance cost in hot code. And there's always a binary size impact. |
I imagined that would be the case (2), but I wanted to make sure :) |
Thanks for all your advice. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
Result must be same as for the following code:
What did you see instead?
Error: "panic: runtime error: slice bounds out of range"
The text was updated successfully, but these errors were encountered: