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

cmd/compile: missing "slice bounds out of range" runtime panic for stringtobytes conversion of size <=32 unless variable escapes #28863

Closed
RafaelTupynamba opened this issue Nov 19, 2018 · 3 comments

Comments

Projects
None yet
4 participants
@RafaelTupynamba
Copy link

commented Nov 19, 2018

What version of Go are you using (go version)?

$ go version
go version go1.12.1

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Linux, Windows, macOS

What did you do?

https://play.golang.org/p/npT2x8ieAV4

What did you expect to see?

panic: runtime error: slice bounds out of range

What did you see instead?

No panic
Panic only happens if you uncomment last Println

@odeke-em

This comment has been minimized.

Copy link
Member

commented Nov 19, 2018

Thank you for reporting this bug @RafaelTupynamba and welcome to the Go project!

I can reproduce this on Go1.9, Go1.10, Go1.11 and on tip aka Go1.12

package main

import "fmt"

func main() {
	key := []byte("Hello")
	key = key[:16]
	fmt.Println(len(key))
	if false {
		fmt.Println(key)
	}
}

with false it'll print 16
but on changing to true will panic on the line key = key[:16]

I believe that this behavior violates the spec https://golang.org/ref/spec#Run_time_panics

Kindly paging some folks from the compiler crew @randall77 @dr2chase @josharian @griesemer

@odeke-em odeke-em added the NeedsFix label Nov 19, 2018

@odeke-em odeke-em added this to the Go1.12 milestone Nov 19, 2018

@odeke-em odeke-em changed the title "slice bounds out of range" not reported cmd/compile: missing "slice bounds out of range" runtime panic unless variable is printed Nov 19, 2018

@odeke-em odeke-em changed the title cmd/compile: missing "slice bounds out of range" runtime panic unless variable is printed cmd/compile: missing "slice bounds out of range" runtime panic for stringtobytes of size <32 unless variable escapes Nov 19, 2018

@odeke-em odeke-em changed the title cmd/compile: missing "slice bounds out of range" runtime panic for stringtobytes of size <32 unless variable escapes cmd/compile: missing "slice bounds out of range" runtime panic for stringtobytes conversion of size <=32 unless variable escapes Nov 19, 2018

@dominikh

This comment has been minimized.

Copy link
Member

commented Nov 19, 2018

Is this #24163?

@martisch

This comment has been minimized.

Copy link
Member

commented Nov 19, 2018

I do not think there is a spec violation, there is no guarantee how large the slice capacity for "key" will be after the conversion as long as the string content fits. Similar other closed recent issue about this #28817.

Closing as Working As Intended as slice capacities can be effected by various optimization decisions that operate within the language specification. #24163 is still open to have an explicit mention that no capacity is guaranteed in the spec.

@martisch martisch closed this Nov 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.