Skip to content

Commit e6d71ad

Browse files
authored
encoding/proto: do not panic when types do not match (#4218) (#4223)
1 parent 61962d0 commit e6d71ad

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

encoding/proto/proto.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package proto
2222

2323
import (
24+
"fmt"
25+
2426
"github.com/golang/protobuf/proto"
2527
"google.golang.org/grpc/encoding"
2628
)
@@ -36,11 +38,19 @@ func init() {
3638
type codec struct{}
3739

3840
func (codec) Marshal(v interface{}) ([]byte, error) {
39-
return proto.Marshal(v.(proto.Message))
41+
vv, ok := v.(proto.Message)
42+
if !ok {
43+
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
44+
}
45+
return proto.Marshal(vv)
4046
}
4147

4248
func (codec) Unmarshal(data []byte, v interface{}) error {
43-
return proto.Unmarshal(data, v.(proto.Message))
49+
vv, ok := v.(proto.Message)
50+
if !ok {
51+
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
52+
}
53+
return proto.Unmarshal(data, vv)
4454
}
4555

4656
func (codec) Name() string {

0 commit comments

Comments
 (0)