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

substring using [n:] behaves unexpectedly when n is the length of the string #22785

Closed
yuekui2 opened this issue Nov 17, 2017 · 2 comments
Closed

Comments

@yuekui2
Copy link

yuekui2 commented Nov 17, 2017

Please answer these questions before submitting your issue. Thanks!

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

1.8.1

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/kui/Downloads/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build932972831=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

https://play.golang.org/p/80Xf0-hz6h

What did you expect to see?

string substring with index of string length should panic

What did you see instead?

string substring with index of string length does NOT fail, but stringLen + 1 fails.

@bradfitz
Copy link
Contributor

This is working as intended. The bug here is only that this was "unexpected", but rest assured that you can expect this. It's defined in the spec, implemented across multiple compilers, and tested thoroughly.

@ALTree
Copy link
Member

ALTree commented Nov 17, 2017

jid[5:] is a string of length 0. For a[low : high], the spec requires

0 <= low <= high <= len(a)

Note that every relation is a <=. In your case you have low = high (implied) = len(a) = 5, which is permitted. In jid[6:], on the other hand, low = 6 > len(a) = 5, which does not satisfy the relation above and is required to trigger a runtime error (and it does).

@golang golang locked and limited conversation to collaborators Nov 17, 2018
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

4 participants