]> Cypherpunks repositories - gostls13.git/commitdiff
bytes: limit allocation in SplitN
authorPhilippe Antoine <contact@catenacyber.fr>
Tue, 5 Apr 2022 20:28:16 +0000 (20:28 +0000)
committerIan Lance Taylor <iant@golang.org>
Wed, 6 Apr 2022 03:13:34 +0000 (03:13 +0000)
So that bytes.SplitN("", "T", int(144115188075855872)) does not panic.

Change-Id: I7c068852bd708416164fc2ed8b84cf6b2d593666
GitHub-Last-Rev: f8df09d65e2bc889fbd0c736bfb5e9a9078dfced
GitHub-Pull-Request: golang/go#52147
Reviewed-on: https://go-review.googlesource.com/c/go/+/398076
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: mzh <mzh@golangcn.org>
src/bytes/bytes.go
src/bytes/bytes_test.go

index e3dab4d03560b85047a729e3bfce2a177e26fc9f..d9d502927ee9305bdc075896afb87628efd983d6 100644 (file)
@@ -348,6 +348,9 @@ func genSplit(s, sep []byte, sepSave, n int) [][]byte {
        if n < 0 {
                n = Count(s, sep) + 1
        }
+       if n > len(s)+1 {
+               n = len(s) + 1
+       }
 
        a := make([][]byte, n)
        n--
index 2e6ab315404e8ca84367d2e6510571362970bce7..b702efb23933c5b8c41d5b6eedfce9b881f678e1 100644 (file)
@@ -8,6 +8,7 @@ import (
        . "bytes"
        "fmt"
        "internal/testenv"
+       "math"
        "math/rand"
        "reflect"
        "strings"
@@ -723,6 +724,7 @@ var splittests = []SplitTest{
        {"1 2", " ", 3, []string{"1", "2"}},
        {"123", "", 2, []string{"1", "23"}},
        {"123", "", 17, []string{"1", "2", "3"}},
+       {"bT", "T", math.MaxInt / 4, []string{"b", ""}},
 }
 
 func TestSplit(t *testing.T) {