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

Should not appear slice bounds out of range #29833

Closed
kexirong opened this issue Jan 19, 2019 · 7 comments
Closed

Should not appear slice bounds out of range #29833

kexirong opened this issue Jan 19, 2019 · 7 comments

Comments

@kexirong
Copy link

kexirong commented Jan 19, 2019

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

$ go version
go version go1.11.4 linux/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/kk/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/data/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build753918768=/tmp/go-build -gno-record-gcc-switches"

What did you do?

run the code will recurrent

package main

import (
	"errors"
	"fmt"
)

func test() (*byte, error) {
	bs := []*byte{}
	b := byte('a')
	bs = append(bs, &b)
	if len(bs) != 1 {
		return nil, errors.New("")
	}
	r := bs[len(bs)-1]
	bs = bs[0 : len(bs)-1]
	return r, nil
}
func main() {
	_, err := test()
	if err != nil {
		fmt.Println(err)
	}
}

code link://https://play.golang.org/p/-vi8ee_6RWF

What did you expect to see?

Comment out return nil, errors.New("") will to run without error

func test() (*byte, error) {
	bs := []*byte{}
	b := byte('a')
	bs = append(bs, &b)
	if len(bs) != 1 {
		//	return nil, errors.New("")
	}
	r := bs[len(bs)-1]
	bs = bs[0 : len(bs)-1]
	return r, nil
}

What did you see instead?

panic: runtime error: slice bounds out of range

goroutine 1 [running]:
main.test(...)
       main.go:16
main.main()
       main.go:20 +0x120

I can't understand why this is

@mvdan
Copy link
Member

mvdan commented Jan 19, 2019

Commenting out the line doesn't make the program panic: https://play.golang.org/p/VrcWRTHJh2m

Please note that the issue tracker is for bugs in Go. If you have a question about your code, see https://golang.org/wiki/Questions.

@mvdan mvdan closed this as completed Jan 19, 2019
@kexirong
Copy link
Author

kexirong commented Jan 19, 2019

Commenting out the line doesn't make the program panic: https://play.golang.org/p/VrcWRTHJh2m

Please note that the issue tracker is for bugs in Go. If you have a question about your code, see https://golang.org/wiki/Questions.

did you sure the playground is go1.11.4 @mvdan

@mvdan
Copy link
Member

mvdan commented Jan 19, 2019

Yes. I even tried it locally with both go version go1.11.4 linux/amd64 and go version devel +c077c74312 Fri Jan 18 03:50:05 2019 +0000 linux/amd64.

@kexirong
Copy link
Author

are you didn't Commenting out the linereturn nil, errors.New("") when run the code @mvdan

@mvdan
Copy link
Member

mvdan commented Jan 19, 2019

I am commenting it out - see the playground link I pasted earlier.

@kexirong
Copy link
Author

kexirong commented Jan 19, 2019

Thank you , My English is poor,What I want to express is why panic when didn't commenting out the line @mvdan

@mvdan
Copy link
Member

mvdan commented Jan 19, 2019

Ok, now I understand what you meant. The good news is that this is already being tracked in #29503; it will be fixed in the upcoming 1.11.5.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants