]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: extend partially allocated string slices
authorJohan Abildskov <randomsort@gmail.com>
Wed, 22 Mar 2023 06:31:25 +0000 (06:31 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 23 Mar 2023 17:32:41 +0000 (17:32 +0000)
Fixes #59172

Change-Id: I54d5e724f10117a40ec5dd58c810f6bbb2475933
GitHub-Last-Rev: d1a986698c820415b2e0be12141091a3cbf6fde3
GitHub-Pull-Request: golang/go#59173
Reviewed-on: https://go-review.googlesource.com/c/go/+/478215
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
src/encoding/gob/codec_test.go
src/encoding/gob/dec_helpers.go

index 54c356c46447221a9fc05b006b737294054bd4e6..28cd6088af16ff076702d3ecc4f1959131c892ed 100644 (file)
@@ -1544,6 +1544,10 @@ type LargeSliceStruct struct {
        S []StringPair
 }
 
+type LargeSliceString struct {
+       S []string
+}
+
 func testEncodeDecode(t *testing.T, in, out any) {
        t.Helper()
        var b bytes.Buffer
@@ -1592,4 +1596,14 @@ func TestLargeSlice(t *testing.T) {
                rt := &LargeSliceStruct{}
                testEncodeDecode(t, st, rt)
        })
+       t.Run("string", func(t *testing.T) {
+               t.Parallel()
+               s := make([]string, 1<<21)
+               for i := range s {
+                       s[i] = string(rune(i))
+               }
+               st := &LargeSliceString{S: s}
+               rt := &LargeSliceString{}
+               testEncodeDecode(t, st, rt)
+       })
 }
index a09ac8fc1afd93bf4c448c398bcd0b9a8fb7ac81..098ba7254a21ed0f77743c792c4faa210b13c51c 100644 (file)
@@ -358,6 +358,9 @@ func decStringSlice(state *decoderState, v reflect.Value, length int, ovfl error
                if state.b.Len() == 0 {
                        errorf("decoding string array or slice: length exceeds input size (%d elements)", length)
                }
+               if i >= len(slice) {
+                       growSlice(v, &slice, length)
+               }
                u := state.decodeUint()
                n := int(u)
                if n < 0 || uint64(n) != u || n > state.b.Len() {