]> Cypherpunks.ru repositories - gostls13.git/commitdiff
strings: limits allocation size for SplitN
authorPhilippe Antoine <contact@catenacyber.fr>
Sun, 20 Mar 2022 21:34:42 +0000 (21:34 +0000)
committerTobias Klauser <tobias.klauser@gmail.com>
Thu, 31 Mar 2022 05:24:51 +0000 (05:24 +0000)
So that `strings.SplitN("", "T", int(144115188075855872))` does not panic.

Change-Id: Iea00417e61780bcaf0fee02fa2b18026d89bc545
GitHub-Last-Rev: d1f45b44a8011ddb27c71e1bc9983b62b5d3d771
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>

src/strings/strings.go
src/strings/strings_test.go

index 5793d9e26f4ed86bf203379c44fecad8cb646cbf..ed3184b59c8fde380246fa229a7c9251d49f6cc4 100644 (file)
@@ -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
index 0f30ca738e6754728fe28fe66e4ed046d08afe89..9e7fb85ddf0c4455ca31ee6eea61178e3b3df93b 100644 (file)
@@ -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) {