go: cmd/compile: missing bounds checks in 1.11

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

go version go1.11 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/pontus/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pontus/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-build210363627=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I’ve narrowed it down to this code: https://play.golang.org/p/3543rqBvJ74

package main

import (
	"fmt"
)

type S struct {
}

type X []S

func (x *X) Pop() S {
	out := (*x)[len(*x)-1]
	*x = (*x)[:len(*x)-1]
	return out
}

func main() {
	mx := X{S{}}
	fmt.Printf("Pre: %v, len: %v\n", mx, len(mx))
	mx.Pop()
	fmt.Printf("Mid: %v, len: %v\n", mx, len(mx))
	mx.Pop()
	fmt.Printf("Post: %v, len: %v\n", mx, len(mx))
}

What did you expect to see?

The playground output:

Pre: [{}], len: 1
Mid: [], len: 0
panic: runtime error: index out of range

That’s what go version 1.10 darwin/amd64 outputs too.

What did you see instead?

Pre: [{}], len: 1
Mid: [], len: 0
Post: [], len: -1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 15 (10 by maintainers)

Most upvoted comments

Eheh we did the same analysis at the same time and mailed an identical CL 👍 🥇

Duplicate of #27251?