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

slice expression with low-index out of range and omitted high index doesn't give "slice bounds out of range" error #28731

Closed
dencoded opened this issue Nov 12, 2018 · 2 comments

Comments

@dencoded
Copy link

@dencoded dencoded commented Nov 12, 2018

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

$ go version
go version go1.11.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/dencoded/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/dencoded/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9z/xzkhmlmx63nc49srq6glx6cw0000gn/T/go-build613348640=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/GrgeaBAXGkp
This happens only if we specify low-index as len of slice

i.e. this gives proper error:
https://play.golang.org/p/Ax4khdrwotW

What did you expect to see?

panic: runtime error: slice bounds out of range

What did you see instead?

len: 1
cap: 1
[]
@dominikh

This comment has been minimized.

Copy link
Member

@dominikh dominikh commented Nov 12, 2018

This is working as intended:

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.

@dominikh dominikh closed this Nov 12, 2018
@dencoded

This comment has been minimized.

Copy link
Author

@dencoded dencoded commented Nov 12, 2018

@dominikh well, if it is by design I've learned something new today 👍

@golang golang locked and limited conversation to collaborators Nov 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants
You can’t perform that action at this time.