mirror of
https://github.com/golang/go
synced 2024-11-12 09:30:25 -07:00
encoding/gob: handle interface types in isZero() by returning true for nil interfaces.
Fixes #7741. LGTM=r R=golang-codereviews, r CC=golang-codereviews https://golang.org/cl/96830044
This commit is contained in:
parent
2674efbdf4
commit
7faf72bd0d
@ -491,7 +491,7 @@ func isZero(val reflect.Value) bool {
|
||||
return !val.Bool()
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
return val.Complex() == 0
|
||||
case reflect.Chan, reflect.Func, reflect.Ptr:
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Ptr:
|
||||
return val.IsNil()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return val.Int() == 0
|
||||
|
@ -705,13 +705,14 @@ func TestGobEncoderExtraIndirect(t *testing.T) {
|
||||
}
|
||||
|
||||
// Another bug: this caused a crash with the new Go1 Time type.
|
||||
// We throw in a gob-encoding array, to test another case of isZero
|
||||
|
||||
// We throw in a gob-encoding array, to test another case of isZero,
|
||||
// and a struct containing an nil interface, to test a third.
|
||||
type isZeroBug struct {
|
||||
T time.Time
|
||||
S string
|
||||
I int
|
||||
A isZeroBugArray
|
||||
F isZeroBugInterface
|
||||
}
|
||||
|
||||
type isZeroBugArray [2]uint8
|
||||
@ -731,8 +732,20 @@ func (a *isZeroBugArray) GobDecode(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type isZeroBugInterface struct {
|
||||
I interface{}
|
||||
}
|
||||
|
||||
func (i isZeroBugInterface) GobEncode() (b []byte, e error) {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
func (i *isZeroBugInterface) GobDecode(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestGobEncodeIsZero(t *testing.T) {
|
||||
x := isZeroBug{time.Now(), "hello", -55, isZeroBugArray{1, 2}}
|
||||
x := isZeroBug{time.Now(), "hello", -55, isZeroBugArray{1, 2}, isZeroBugInterface{}}
|
||||
b := new(bytes.Buffer)
|
||||
enc := NewEncoder(b)
|
||||
err := enc.Encode(x)
|
||||
|
Loading…
Reference in New Issue
Block a user