-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Inlining prevents a slice panic #28817
Comments
I think this is due to the allowed semantics of If you replace |
Ah, you are correct. It appears that in the inlined version the capacity is always 32 (until your input string is larger) and in the not inlined version it's 8. Inlining decisions indeed affect the capacity of the byte slices. And it appears the increment of the capacity depends on inlining decisions as well. This holds true even when converted from user input. So it's possible that a user could pass in a string and the failure of the program given various inputs could depend on whether or not the code was inlined. Would the recommended way to handle this be to slice the input and enforce the capacity to be equal to the length of the input to ensure you cannot read past the end of the buffer? (something like Or should this be handled entirely by logic to ensure the read position isn't |
That's a good backstop. Your logic of course should make this not matter. It's only when the logic is wrong that this behavior is noticeable. The other option is don't covert to |
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?
I was writing a lexer which unknowingly had a slicing memory error. It was working when it should not have been. I made a change to assign the tokens to a property on the lexer struct and it caused panics to appear causing the tests to fail.
I've created a reduced test case and it looks as if a decision comes down to inlining and when inlined the slicing panic does not occur as it should.
Inlined version that does not panic like it should:
https://play.golang.org/p/bdDhQWRj9GC
Not inlined version that does panic:
https://play.golang.org/p/r4KwyND4XdW
If you'd like the full context around the bug you can find the repo here:
https://github.com/pittfit/ortho/blob/5257d3027bda4989ff2e51edea082a95fa135263/lexer/lexer.go#L64
What did you expect to see?
A panic in both versions. Inlining should not affect program correctness.
What did you see instead?
A panic in the not inlined version only.
The text was updated successfully, but these errors were encountered: