]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: fix panic when decoding []byte to incompatible slice types
authorAlexey Borzenkov <snaury@gmail.com>
Mon, 9 Jan 2012 20:52:03 +0000 (12:52 -0800)
committerRob Pike <r@golang.org>
Mon, 9 Jan 2012 20:52:03 +0000 (12:52 -0800)
Fixes #2662.

R=golang-dev, rogpeppe, r, r
CC=golang-dev, r, rogpeppe
https://golang.org/cl/5515050

src/pkg/encoding/gob/decode.go
src/pkg/encoding/gob/encoder_test.go

index ba1f2eb813078b823926d82a17d01e37dda492e9..4d1325d176c61cc0455047559e36725c17cfb1ed 100644 (file)
@@ -1039,9 +1039,9 @@ func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[re
                // Extract and compare element types.
                var sw *sliceType
                if tt, ok := builtinIdToType[fw]; ok {
-                       sw = tt.(*sliceType)
-               } else {
-                       sw = dec.wireType[fw].SliceT
+                       sw, _ = tt.(*sliceType)
+               } else if wire != nil {
+                       sw = wire.SliceT
                }
                elem := userType(t.Elem()).base
                return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress)
index cd1500d0772546afdad8427c3bb9b3cf5807a0b3..7a30f9107e636638e6d666fce46792d5bb639be2 100644 (file)
@@ -678,3 +678,11 @@ func TestUnexportedChan(t *testing.T) {
                t.Fatalf("error encoding unexported channel: %s", err)
        }
 }
+
+func TestSliceIncompatibility(t *testing.T) {
+       var in = []byte{1, 2, 3}
+       var out []int
+       if err := encAndDec(in, &out); err == nil {
+               t.Error("expected compatibility error")
+       }
+}