mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
d166a369a8
commit
7b40b0c3a3
4 changed files with 123 additions and 0 deletions
|
|
@ -365,7 +365,20 @@ func Map(mapping func(r rune) rune, s []byte) []byte {
|
|||
}
|
||||
|
||||
// Repeat returns a new byte slice consisting of count copies of b.
|
||||
//
|
||||
// It panics if count is negative or if
|
||||
// the result of (len(b) * count) overflows.
|
||||
func Repeat(b []byte, count int) []byte {
|
||||
// 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("bytes: negative Repeat count")
|
||||
} else if count > 0 && len(b)*count/count != len(b) {
|
||||
panic("bytes: Repeat count causes overflow")
|
||||
}
|
||||
|
||||
nb := make([]byte, len(b)*count)
|
||||
bp := copy(nb, b)
|
||||
for bp < len(nb) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue