From ba626ac327f45a6d9d211fddd5b48e321fa0702a Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Fri, 18 Aug 2023 13:39:57 +0800 Subject: encoding/gob: prevent panic from index out of range in Decoder.typeString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I believe this bug is introduced by CL 460543 which optimizes the allocations by changing the type of `idToType` from map to slice, but didn't update the access code in `Decoder.typeString` that is safe for map but not for slice. Fixes #62117 Change-Id: I0f2e4cc2f34c54dada1f83458ba512a6fde6dcbe Reviewed-on: https://go-review.googlesource.com/c/go/+/520757 TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Run-TryBot: Andy Pan Auto-Submit: Ian Lance Taylor Reviewed-by: Daniel Martí Reviewed-by: Dmitri Shuralyov Run-TryBot: Ian Lance Taylor --- src/encoding/gob/codec_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/encoding/gob/codec_test.go') diff --git a/src/encoding/gob/codec_test.go b/src/encoding/gob/codec_test.go index 11a38f5f58..ec56ad50b2 100644 --- a/src/encoding/gob/codec_test.go +++ b/src/encoding/gob/codec_test.go @@ -1616,3 +1616,15 @@ func TestLargeSlice(t *testing.T) { testEncodeDecode(t, st, rt) }) } + +func TestLocalRemoteTypesMismatch(t *testing.T) { + // Test data is from https://go.dev/issue/62117. + testData := []byte{9, 127, 3, 1, 2, 255, 128, 0, 0, 0, 3, 255, 128, 0} + + var v []*struct{} + buf := bytes.NewBuffer(testData) + err := NewDecoder(buf).Decode(&v) + if err == nil { + t.Error("Encode/Decode: expected error but got err == nil") + } +} -- cgit v1.3-5-g9baa