]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: arrays are zero only if their elements are zero
authorRob Pike <r@golang.org>
Fri, 16 Dec 2011 19:52:58 +0000 (11:52 -0800)
committerRob Pike <r@golang.org>
Fri, 16 Dec 2011 19:52:58 +0000 (11:52 -0800)
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5494059

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

index 11afa02ea5aa4a42a4b7edb45544364e83d3fe56..f05b17c309699e6684fbd75722680f6ba842118e 100644 (file)
@@ -469,7 +469,14 @@ func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv reflect.Value) {
 // isZero returns whether the value is the zero of its type.
 func isZero(val reflect.Value) bool {
        switch val.Kind() {
-       case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
+       case reflect.Array:
+               for i := 0; i < val.Len(); i++ {
+                       if !isZero(val.Index(i)) {
+                               return false
+                       }
+               }
+               return true
+       case reflect.Map, reflect.Slice, reflect.String:
                return val.Len() == 0
        case reflect.Bool:
                return !val.Bool()
index 5cab4115919bfb44486000cc1635e0d4bc83567c..b8dfeeb5156d0e066794ef749c3a6df588b8006d 100644 (file)
@@ -529,28 +529,48 @@ 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
 
-type TimeBug struct {
+type isZeroBug struct {
        T time.Time
        S string
        I int
+       A isZeroBugArray
 }
 
-func TestGobEncodeTime(t *testing.T) {
-       x := TimeBug{time.Now(), "hello", -55}
+type isZeroBugArray [2]uint8
+
+// Receiver is value, not pointer, to test isZero of array.
+func (a isZeroBugArray) GobEncode() (b []byte, e error) {
+       b = append(b, a[:]...)
+       return b, nil
+}
+
+func (a *isZeroBugArray) GobDecode(data []byte) error {
+       println("DECODE")
+       if len(data) != len(a) {
+               return io.EOF
+       }
+       a[0] = data[0]
+       a[1] = data[1]
+       return nil
+}
+
+func TestGobEncodeIsZero(t *testing.T) {
+       x := isZeroBug{time.Now(), "hello", -55, isZeroBugArray{1, 2}}
        b := new(bytes.Buffer)
        enc := NewEncoder(b)
        err := enc.Encode(x)
        if err != nil {
                t.Fatal("encode:", err)
        }
-       var y TimeBug
+       var y isZeroBug
        dec := NewDecoder(b)
        err = dec.Decode(&y)
        if err != nil {
                t.Fatal("decode:", err)
        }
        if x != y {
-               t.Fatal("%v != %v", x, y)
+               t.Fatalf("%v != %v", x, y)
        }
 }