diff options
| author | Emmanuel Odeke <emm.odeke@gmail.com> | 2016-09-28 01:54:38 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-10-01 15:47:35 +0000 |
| commit | 7b40b0c3a332cbfaa1eb17bdafd2ddf12119ec45 (patch) | |
| tree | b7472b36ac1bd223188ba3ba11082b0b328f5f3f /src/strings/strings.go | |
| parent | d166a369a89ef2d81efdc5d49fa782ee1c0186c4 (diff) | |
| download | go-7b40b0c3a332cbfaa1eb17bdafd2ddf12119ec45.tar.xz | |
strings, bytes: panic if Repeat overflows or if given a negative count
Panic if Repeat is given a negative count or
if the value of (len(*) * count) is detected
to overflow.
We panic because we cannot change the
signature of Repeat to return an error.
Fixes #16237
Change-Id: I9f5ba031a5b8533db0582d7a672ffb715143f3fb
Reviewed-on: https://go-review.googlesource.com/29954
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/strings/strings.go')
| -rw-r--r-- | src/strings/strings.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go index c5355db9a2..10922f3c1d 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -418,7 +418,20 @@ func Map(mapping func(rune) rune, s string) string { } // Repeat returns a new string consisting of count copies of the string s. +// +// It panics if count is negative or if +// the result of (len(s) * count) overflows. func Repeat(s string, count int) string { + // Since we cannot return an error on overflow, + // we should panic if the repeat will generate + // an overflow. + // See Issue golang.org/issue/16237 + if count < 0 { + panic("strings: negative Repeat count") + } else if count > 0 && len(s)*count/count != len(s) { + panic("strings: Repeat count causes overflow") + } + b := make([]byte, len(s)*count) bp := copy(b, s) for bp < len(b) { |
