mirror of
https://github.com/golang/go
synced 2024-11-17 05:04:54 -07:00
strings: limits allocation size for SplitN
So that `strings.SplitN("", "T", int(144115188075855872))` does not panic.
Change-Id: Iea00417e61780bcaf0fee02fa2b18026d89bc545
GitHub-Last-Rev: d1f45b44a8
GitHub-Pull-Request: golang/go#51755
Reviewed-on: https://go-review.googlesource.com/c/go/+/393654
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
parent
90b29e1865
commit
cc46cac3bc
@ -244,6 +244,9 @@ func genSplit(s, sep string, sepSave, n int) []string {
|
||||
n = Count(s, sep) + 1
|
||||
}
|
||||
|
||||
if n > len(s)+1 {
|
||||
n = len(s) + 1
|
||||
}
|
||||
a := make([]string, n)
|
||||
n--
|
||||
i := 0
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@ -404,6 +405,7 @@ var splittests = []SplitTest{
|
||||
{faces, "~", -1, []string{faces}},
|
||||
{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
|
||||
{"1 2", " ", 3, []string{"1", "2"}},
|
||||
{"", "T", math.MaxInt / 4, []string{""}},
|
||||
}
|
||||
|
||||
func TestSplit(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user