You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What version of Go are you using (go version)?
go version go1.6.2 linux/amd64
What operating system and processor architecture are you using (go env)?
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pat/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
What did you expect to see?
A panic because the program tries to allocate too much memory.
What did you see instead?
Both bytes.Repeat and strings.Repeat appear to succeed, but the result is shorter than the input string.
Looking at https://golang.org/src/bytes/bytes.go?s=9819:9858#L381, the cause seems to be that len(b)*count overflows, and happens to produce a result that is a small positive integer. Perhaps the routine should check for overflow and panic if one occurs.
The text was updated successfully, but these errors were encountered:
It's better to panic than to return the wrong answer. Given that we have no way to return an error from these functions, I think panicing is our only option here.
I tried https://play.golang.org/p/qQ37uU4mGT to see what might happen in other cases that require more memory than is available. On the playground, that produces a fatal runtime error that seems not to be a panic:
2147483647
runtime: memory allocated by OS (0x7ef60000) not in usable range [0x10400000,0x90400000)
runtime: out of memory: cannot allocate 2147483648-byte block (1048576 in use)
fatal error: out of memory
...
On my desktop, with 64 bit ints, the same program produces a panic:
9223372036854775807
panic: runtime error: makeslice: len out of range
go version
)?go version go1.6.2 linux/amd64
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pat/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
https://play.golang.org/p/o-5UiG_cKc
A panic because the program tries to allocate too much memory.
Both bytes.Repeat and strings.Repeat appear to succeed, but the result is shorter than the input string.
Looking at https://golang.org/src/bytes/bytes.go?s=9819:9858#L381, the cause seems to be that len(b)*count overflows, and happens to produce a result that is a small positive integer. Perhaps the routine should check for overflow and panic if one occurs.
The text was updated successfully, but these errors were encountered: