]> Cypherpunks.ru repositories - gostls13.git/commitdiff
regexp: limit the capacity of slices of bytes returned by FindX
authorFrancesc Campoy <campoy@golang.org>
Mon, 11 Feb 2019 14:33:12 +0000 (16:33 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 26 Feb 2019 23:51:17 +0000 (23:51 +0000)
This change limits the capacity of the slices of bytes returned by:

- Find
- FindAll
- FindAllSubmatch

to be the same as their length.

Fixes #30169

Change-Id: I07b632757d2bfeab42fce0d42364e2a16c597360
Reviewed-on: https://go-review.googlesource.com/c/161877
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/regexp/find_test.go
src/regexp/regexp.go

index e07eb7d5c05059c3ba0ee74477a3179ebccdf416..87c49b074fa48655b3510cc31443bd645436cb6d 100644 (file)
@@ -161,6 +161,9 @@ func TestFind(t *testing.T) {
                        t.Errorf("expected match; got none: %s", test)
                case test.matches != nil && result != nil:
                        expect := test.text[test.matches[0][0]:test.matches[0][1]]
+                       if len(result) != cap(result) {
+                               t.Errorf("expected capacity %d got %d: %s", len(result), cap(result), test)
+                       }
                        if expect != string(result) {
                                t.Errorf("expected %q got %q: %s", expect, result, test)
                        }
@@ -242,9 +245,13 @@ func TestFindAll(t *testing.T) {
                                continue
                        }
                        for k, e := range test.matches {
+                               got := result[k]
+                               if len(got) != cap(got) {
+                                       t.Errorf("match %d: expected capacity %d got %d: %s", k, len(got), cap(got), test)
+                               }
                                expect := test.text[e[0]:e[1]]
-                               if expect != string(result[k]) {
-                                       t.Errorf("match %d: expected %q got %q: %s", k, expect, result[k], test)
+                               if expect != string(got) {
+                                       t.Errorf("match %d: expected %q got %q: %s", k, expect, got, test)
                                }
                        }
                }
@@ -323,9 +330,14 @@ func testSubmatchBytes(test *FindTest, n int, submatches []int, result [][]byte,
                        }
                        continue
                }
+               got := result[k/2]
+               if len(got) != cap(got) {
+                       t.Errorf("match %d: expected capacity %d got %d: %s", n, len(got), cap(got), test)
+                       return
+               }
                expect := test.text[submatches[k]:submatches[k+1]]
-               if expect != string(result[k/2]) {
-                       t.Errorf("match %d: expected %q got %q: %s", n, expect, result, test)
+               if expect != string(got) {
+                       t.Errorf("match %d: expected %q got %q: %s", n, expect, got, test)
                        return
                }
        }
index 38b3c86d9f683a3a1e6803ff8c94a17dc24493a4..88122d42508476bed1ade0f4552b4f55ea0b4c71 100644 (file)
@@ -761,7 +761,7 @@ func (re *Regexp) Find(b []byte) []byte {
        if a == nil {
                return nil
        }
-       return b[a[0]:a[1]]
+       return b[a[0]:a[1]:a[1]]
 }
 
 // FindIndex returns a two-element slice of integers defining the location of
@@ -829,7 +829,7 @@ func (re *Regexp) FindSubmatch(b []byte) [][]byte {
        ret := make([][]byte, 1+re.numSubexp)
        for i := range ret {
                if 2*i < len(a) && a[2*i] >= 0 {
-                       ret[i] = b[a[2*i]:a[2*i+1]]
+                       ret[i] = b[a[2*i]:a[2*i+1]:a[2*i+1]]
                }
        }
        return ret
@@ -1025,7 +1025,7 @@ func (re *Regexp) FindAll(b []byte, n int) [][]byte {
                if result == nil {
                        result = make([][]byte, 0, startSize)
                }
-               result = append(result, b[match[0]:match[1]])
+               result = append(result, b[match[0]:match[1]:match[1]])
        })
        return result
 }
@@ -1100,7 +1100,7 @@ func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte {
                slice := make([][]byte, len(match)/2)
                for j := range slice {
                        if match[2*j] >= 0 {
-                               slice[j] = b[match[2*j]:match[2*j+1]]
+                               slice[j] = b[match[2*j]:match[2*j+1]:match[2*j+1]]
                        }
                }
                result = append(result, slice)