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
strings: Repeat: runtime error: makeslice: len out of range #25709
Comments
Can you please post the entire panic message. Thanks.
… On 3 Jun 2018, at 17:30, Bjørn Erik Pedersen ***@***.***> wrote:
▶ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bep/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bep/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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/n6/s_85mm8d31j6yctssnmn_g1r0000gn/T/go-build040656216=/tmp/go-build -gno-record-gcc-switches -fno-common"
package main
import (
"math"
"strings"
)
func main() {
strings.Repeat("h", math.MaxInt64)
}
Panics on my MacOs installation:
strings.Repeat(0x1071f07, 0x1, 0x7fffffffffffffff, 0xc42004bf78, 0xc42007e058)
/usr/local/go/src/strings/strings.go:543 +0x83
On playground it panics with another error: https://play.golang.org/
constant 9223372036854775807 overflows int
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
panic: runtime error: makeslice: len out of range
goroutine 1 [running]:
strings.Repeat(0x1071f07, 0x1, 0x7fffffffffffffff, 0xc42004bf78, 0xc42007e058)
/usr/local/go/src/strings/strings.go:543 +0x83
main.main()
/Users/bep/go/src/github.com/bep/temp/main.go:9 +0x45 |
The playground uses nacl/amd64p32 and only has 32bit ints and thereby knows that math.MaxInt64 as a const will definitily be to large to fit in an int in the call to to strings.Repeat. The max length of a slice that can be theoretically attempted to be allocated depends on the platform and the element size. The compiler does not propagate the "h" and math.MaxInt64 all the way to runtime.makeslice and checks this at compile time. Therefore the runtime checks this before attempting to allocate memory. Most amd64 machines have a 48bit address space (some newer ones seem to exist with 57bit?) So the runtime checks if the length of the slice to be created (depending on the element size) could be tracked at all (regardless if available ram which might be even smaller). And math.MaxInt64 wont be able to be handled by any existing amd64 machine and would require 16 exibytes of adressable ram. |
What did you expect this code would do?
… On 3 Jun 2018, at 18:08, Bjørn Erik Pedersen ***@***.***> wrote:
panic: runtime error: makeslice: len out of range
goroutine 1 [running]:
strings.Repeat(0x1071f07, 0x1, 0x7fffffffffffffff, 0xc42004bf78, 0xc42007e058)
/usr/local/go/src/strings/strings.go:543 +0x83
main.main()
/Users/bep/go/src/github.com/bep/temp/main.go:9 +0x45
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I didn't expect this to work, I just expected a different error message. If I try: package main
import (
"math"
"strings"
)
func main() {
strings.Repeat("hh", math.MaxInt64)
} I get: panic: strings: Repeat count causes overflow
goroutine 1 [running]:
strings.Repeat(0x1071f5b, 0x2, 0x7fffffffffffffff, 0xc42004bf78, 0xc42007e058)
/usr/local/go/src/strings/strings.go:540 +0x1a9
main.main()
/Users/bep/go/src/github.com/bep/temp/main.go:9 +0x45 |
Repeat can know if the len of the resulting string overflows int and therefore it would never work aside from any memory limits due to limits of int. The library functions generally do not check if the specific platform is able to allocate such large slices themselves or if there is enough available ram. While the makeslice error could be more informative (it handles all cases e.g. negative len and to large len equally) the panic message is I think set in stone due to the GO1 compatibility guarantee. |
Fair enough. |
Panics on my MacOs installation:
panic: runtime error: makeslice: len out of range goroutine 1 [running]: strings.Repeat(0x1071f07, 0x1, 0x7fffffffffffffff, 0xc42004bf78, 0xc42007e058) /usr/local/go/src/strings/strings.go:543 +0x83 main.main()
On playground it panics with a different error: https://play.golang.org/
The text was updated successfully, but these errors were encountered: