encoding/gob: clean up decoderMap after errBadType

When decoding an invalid typeId the associated *decEngine was not
removed from decoderMap. If the decoder was run again on the same input
a nil *decEngine was found in the map and assumed to be initialized,
resulting in a panic.

Fixes #9649

Change-Id: I5bb51808362a21c09228c2705a658f073e5b59b3
Reviewed-on: https://go-review.googlesource.com/3509
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Håvard Haugen 2015-01-28 23:07:05 +01:00 committed by Rob Pike
parent b28802d2f1
commit 8e6cf5f70c
2 changed files with 22 additions and 1 deletions

View file

@ -1473,3 +1473,22 @@ func TestFuzzOneByte(t *testing.T) {
}
}
}
// Don't crash, just give error with invalid type id.
// Issue 9649.
func TestErrorInvalidTypeId(t *testing.T) {
data := []byte{0x01, 0x00, 0x01, 0x00}
d := NewDecoder(bytes.NewReader(data))
// When running d.Decode(&foo) the first time the decoder stops
// after []byte{0x01, 0x00} and reports an errBadType. Running
// d.Decode(&foo) again on exactly the same input sequence should
// give another errBadType, but instead caused a panic because
// decoderMap wasn't cleaned up properly after the first error.
for i := 0; i < 2; i++ {
var foo struct{}
err := d.Decode(&foo)
if err != errBadType {
t.Fatal("decode: expected %s, got %s", errBadType, err)
}
}
}