Navigation Menu

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

strings: Repeat: runtime error: makeslice: len out of range #25709

Closed
bep opened this issue Jun 3, 2018 · 7 comments
Closed

strings: Repeat: runtime error: makeslice: len out of range #25709

bep opened this issue Jun 3, 2018 · 7 comments

Comments

@bep
Copy link
Contributor

bep commented Jun 3, 2018

▶ go version
go version go1.10.1 darwin/amd64
▶ 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:

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/

constant 9223372036854775807 overflows int
@davecheney
Copy link
Contributor

davecheney commented Jun 3, 2018 via email

@bep
Copy link
Contributor Author

bep commented Jun 3, 2018

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

@martisch
Copy link
Contributor

martisch commented Jun 3, 2018

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.
Therefore its "len out of range" of the supported length for a byte sized slice.

@davecheney
Copy link
Contributor

davecheney commented Jun 3, 2018 via email

@bep
Copy link
Contributor Author

bep commented Jun 3, 2018

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

@martisch
Copy link
Contributor

martisch commented Jun 3, 2018

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.

@bep
Copy link
Contributor Author

bep commented Jun 3, 2018

Fair enough.

@bep bep closed this as completed Jun 3, 2018
@golang golang locked and limited conversation to collaborators Jun 3, 2019
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