goprotobuf: Return a clear failure if a non pointer-to-struct is passed to Marshal/Unmarshal.
R=r
CC=golang-dev
http://codereview.appspot.com/5989064
diff --git a/proto/all_test.go b/proto/all_test.go
index 374000e..edf0ca5 100644
--- a/proto/all_test.go
+++ b/proto/all_test.go
@@ -1214,12 +1214,19 @@
}
}
-// Check that passing a struct to Marshal returns a good error,
-// rather than panicking.
-func TestStructMarshaling(t *testing.T) {
- _, err := Marshal(OtherMessage{})
- if err != ErrNotPtr {
- t.Errorf("got %v, expected %v", err, ErrNotPtr)
+// Check that passing things other than pointer to struct to Marshal
+// returns a good error, rather than panicking.
+func TestStructTyping(t *testing.T) {
+ om := &OtherMessage{}
+ bad := [...]interface{}{*om, &om}
+ for _, pb := range bad {
+ _, err := Marshal(pb)
+ if err != ErrNotPtr {
+ t.Errorf("marshaling %T: got %v, expected %v", pb, err, ErrNotPtr)
+ }
+ if err := Unmarshal([]byte{}, pb); err != ErrNotPtr {
+ t.Errorf("unmarshaling %T: got %v, expected %v", pb, err, ErrNotPtr)
+ }
}
}
diff --git a/proto/encode.go b/proto/encode.go
index a64de1f..1248e97 100644
--- a/proto/encode.go
+++ b/proto/encode.go
@@ -62,8 +62,8 @@
// ErrNil is the error returned if Marshal is called with nil.
ErrNil = errors.New("proto: Marshal called with nil")
- // ErrNotPtr is the error returned if Marshal is called with a non-pointer.
- ErrNotPtr = errors.New("proto: Marshal called with a non-pointer")
+ // ErrNotPtr is the error returned if Marshal is called with something other than a pointer to a struct.
+ ErrNotPtr = errors.New("proto: Marshal called with something other than a pointer to a struct")
)
// The fundamental encoders that put bytes on the wire.
diff --git a/proto/properties.go b/proto/properties.go
index c68936d..1318759 100644
--- a/proto/properties.go
+++ b/proto/properties.go
@@ -521,7 +521,7 @@
}
// get the reflect type of the pointer to the struct.
t = reflect.TypeOf(pb)
- if t.Kind() != reflect.Ptr {
+ if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
err = ErrNotPtr
return
}