]> Cypherpunks repositories - gostls13.git/commitdiff
can't encode array or slice - catch in sendType rather than failing in Encode
authorRob Pike <r@golang.org>
Mon, 31 Aug 2009 02:46:35 +0000 (19:46 -0700)
committerRob Pike <r@golang.org>
Mon, 31 Aug 2009 02:46:35 +0000 (19:46 -0700)
R=rsc
DELTA=38  (33 added, 3 deleted, 2 changed)
OCL=34101
CL=34104

src/pkg/gob/encoder.go
src/pkg/gob/encoder_test.go

index 2d84028aaf2ce007bcbc16c23342c2f83c8a2f63..f4a5610295d237a8a2cfd87a25cdfa7374d0c2bd 100644 (file)
@@ -241,22 +241,30 @@ func (enc *Encoder) sendType(origt reflect.Type) {
 
        // We only send structs - everything else is basic or an error
        switch t := rt.(type) {
-       case *reflect.StructType:
+       case *reflect.StructType:       // TODO: when compiler handles type lists, can fold these
                break;  // we handle these
        case *reflect.ChanType:
                enc.badType(rt);
                return;
-       case *reflect.MapType:
+       case *reflect.FuncType:
                enc.badType(rt);
                return;
-       case *reflect.FuncType:
+       case *reflect.MapType:
                enc.badType(rt);
                return;
        case *reflect.InterfaceType:
                enc.badType(rt);
                return;
+       // Array and slice types are not sent, only their element types.
+       // If we see one here it's user error.
+       case *reflect.ArrayType:
+               enc.badType(rt);
+               return;
+       case *reflect.SliceType:
+               enc.badType(rt);
+               return;
        default:
-               return; // basic, array, etc; not a type to be sent.
+               return; // basic, not a type to be sent.
        }
 
        // Have we already sent this type?  This time we ask about the base type.
index 534816fc849c16b7922232557556186c3c092d07..4cad834d093324c5b362c7020c5e6dfee1876cbd 100644 (file)
@@ -243,3 +243,25 @@ func TestBadData(t *testing.T) {
        corruptDataCheck("\x7Fhi", io.ErrUnexpectedEOF, t);
        corruptDataCheck("\x03now is the time for all good men", errBadType, t);
 }
+
+// Types not supported by the Encoder (only structs work at the top level).
+// Basic types work implicitly.
+var unsupportedValues = []interface{} {
+       []int{ 1, 2, 3 },
+       [3]int{ 1, 2, 3 },
+       make(chan int),
+       func(a int) bool { return true },
+       make(map[string] int),
+       new(interface{}),
+}
+
+func TestUnsupported(t *testing.T) {
+       var b bytes.Buffer;
+       enc := NewEncoder(&b);
+       for _, v := range unsupportedValues {
+               err := enc.Encode(v);
+               if err == nil {
+                       t.Errorf("expected error for %T; got none", v)
+               }
+       }
+}