]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: handle interface types in isZero() by returning true for nil...
authorJonathan Allie <jonallie@google.com>
Sat, 26 Apr 2014 16:25:16 +0000 (10:25 -0600)
committerRob Pike <r@golang.org>
Sat, 26 Apr 2014 16:25:16 +0000 (10:25 -0600)
Fixes #7741.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/96830044

src/pkg/encoding/gob/encode.go
src/pkg/encoding/gob/gobencdec_test.go

index d158b6442a8c7f9f3348d3443683cdfbe2d1504a..7831c02d139a2e66c44af9ecd8477eeaa80fe99f 100644 (file)
@@ -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
index 0193e2b67d4312b452676cbd787b0563eed55998..157b7723a75ff6774264b56882bc4bcdbe548431 100644 (file)
@@ -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)