mirror of
https://github.com/golang/go
synced 2024-11-26 14:26:51 -07:00
encoding/gob: ensure "duplicate type received" decoder errors surface up
Previously re-using a decoder with a new stream resulted in a confusing "extra data in buffer" error message. Change-Id: Ia4c4c3a2d4b63c59e37e53faa61a500d5ff6e5f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/297949 Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
This commit is contained in:
parent
83e79c7b14
commit
b3235b75d1
@ -152,6 +152,9 @@ func (dec *Decoder) decodeTypeSequence(isInterface bool) typeId {
|
||||
}
|
||||
// Type definition for (-id) follows.
|
||||
dec.recvType(-id)
|
||||
if dec.err != nil {
|
||||
break
|
||||
}
|
||||
// When decoding an interface, after a type there may be a
|
||||
// DelimitedValue still in the buffer. Skip its count.
|
||||
// (Alternatively, the buffer is empty and the byte count
|
||||
|
@ -1127,3 +1127,28 @@ func TestBadData(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeErrorMultipleTypes(t *testing.T) {
|
||||
type Test struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
var b bytes.Buffer
|
||||
NewEncoder(&b).Encode(Test{"one", 1})
|
||||
|
||||
var result, result2 Test
|
||||
dec := NewDecoder(&b)
|
||||
err := dec.Decode(&result)
|
||||
if err != nil {
|
||||
t.Errorf("decode: unexpected error %v", err)
|
||||
}
|
||||
|
||||
b.Reset()
|
||||
NewEncoder(&b).Encode(Test{"two", 2})
|
||||
err = dec.Decode(&result2)
|
||||
if err == nil {
|
||||
t.Errorf("decode: expected duplicate type error, got nil")
|
||||
} else if !strings.Contains(err.Error(), "duplicate type") {
|
||||
t.Errorf("decode: expected duplicate type error, got %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user