Commit 793768e9 authored by Alexey Borzenkov's avatar Alexey Borzenkov Committed by Rob Pike

encoding/gob: fix panic when decoding []byte to incompatible slice types

Fixes #2662.

R=golang-dev, rogpeppe, r, r
CC=golang-dev, r, rogpeppe
https://golang.org/cl/5515050
parent 41806ec2
......@@ -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)
......
......@@ -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")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment