Commit 7faf72bd authored by Jonathan Allie's avatar Jonathan Allie Committed by Rob Pike

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
parent 2674efbd
...@@ -491,7 +491,7 @@ func isZero(val reflect.Value) bool { ...@@ -491,7 +491,7 @@ func isZero(val reflect.Value) bool {
return !val.Bool() return !val.Bool()
case reflect.Complex64, reflect.Complex128: case reflect.Complex64, reflect.Complex128:
return val.Complex() == 0 return val.Complex() == 0
case reflect.Chan, reflect.Func, reflect.Ptr: case reflect.Chan, reflect.Func, reflect.Interface, reflect.Ptr:
return val.IsNil() return val.IsNil()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return val.Int() == 0 return val.Int() == 0
......
...@@ -705,13 +705,14 @@ func TestGobEncoderExtraIndirect(t *testing.T) { ...@@ -705,13 +705,14 @@ func TestGobEncoderExtraIndirect(t *testing.T) {
} }
// Another bug: this caused a crash with the new Go1 Time type. // 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 { type isZeroBug struct {
T time.Time T time.Time
S string S string
I int I int
A isZeroBugArray A isZeroBugArray
F isZeroBugInterface
} }
type isZeroBugArray [2]uint8 type isZeroBugArray [2]uint8
...@@ -731,8 +732,20 @@ func (a *isZeroBugArray) GobDecode(data []byte) error { ...@@ -731,8 +732,20 @@ func (a *isZeroBugArray) GobDecode(data []byte) error {
return nil 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) { 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) b := new(bytes.Buffer)
enc := NewEncoder(b) enc := NewEncoder(b)
err := enc.Encode(x) err := enc.Encode(x)
......
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