From 978659a441f327b242616d8f61a4893296403d1d Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 13 Feb 2020 14:57:21 +0300 Subject: [PATCH] client/grpc: fix panic on invalid message (#1191) * client/grpc: fix panic on invalid message * travis: disable lint and race for now Signed-off-by: Vasiliy Tolstov --- codec.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/codec.go b/codec.go index 4b224a7..ff37769 100644 --- a/codec.go +++ b/codec.go @@ -69,15 +69,21 @@ func (w wrapCodec) Unmarshal(data []byte, v interface{}) error { } func (protoCodec) Marshal(v interface{}) ([]byte, error) { - b, ok := v.(*bytes.Frame) - if ok { - return b.Data, nil + switch m := v.(type) { + case *bytes.Frame: + return m.Data, nil + case proto.Message: + return proto.Marshal(m) } - return proto.Marshal(v.(proto.Message)) + return nil, fmt.Errorf("failed to marshal: %v is not type of *bytes.Frame or proto.Message", v) } func (protoCodec) Unmarshal(data []byte, v interface{}) error { - return proto.Unmarshal(data, v.(proto.Message)) + m, ok := v.(proto.Message) + if !ok { + return fmt.Errorf("failed to unmarshal: %v is not type of proto.Message", v) + } + return proto.Unmarshal(data, m) } func (protoCodec) Name() string {