Skip to content

bytes+strings: Repeat can appear to succeed with an unreasonably large count #16237

@pat42smith

Description

@pat42smith
  1. What version of Go are you using (go version)?
    go version go1.6.2 linux/amd64
  2. 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"
  3. What did you do?
    https://play.golang.org/p/o-5UiG_cKc
  4. What did you expect to see?
    A panic because the program tries to allocate too much memory.
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions