internal/impl: handle some dynamic legacy messages
When creating a MessageDescriptor for a legacy message with a
Descriptor method, we call that method on the type's zero value to
get the message's DescriptorProto. Some existing dynamic message
types have a Descriptor method which panics in this case.
Catch the panic and continue as if the Descriptor method wasn't present.
Change-Id: I98d4625d6917cc1ec25737e5670a443f5d02a404
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/206637
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/proto/methods_test.go b/proto/methods_test.go
index cc5dfce..385fcbf 100644
--- a/proto/methods_test.go
+++ b/proto/methods_test.go
@@ -79,3 +79,24 @@
t.Fatalf("proto.Unmarshal(selfMarshaler{}): Marshal method not called")
}
}
+
+type descPanicSelfMarshaler struct{}
+
+const descPanicSelfMarshalerBytes = "bytes"
+
+func (m descPanicSelfMarshaler) Reset() {}
+func (m descPanicSelfMarshaler) ProtoMessage() {}
+func (m descPanicSelfMarshaler) Descriptor() ([]byte, []int) { panic("Descriptor method panics") }
+func (m descPanicSelfMarshaler) String() string { return "descPanicSelfMarshaler{}" }
+func (m descPanicSelfMarshaler) Marshal() ([]byte, error) {
+ return []byte(descPanicSelfMarshalerBytes), nil
+}
+
+func TestSelfMarshalerDescriptorPanics(t *testing.T) {
+ m := descPanicSelfMarshaler{}
+ got, err := proto.Marshal(impl.Export{}.MessageOf(m).Interface())
+ want := []byte(descPanicSelfMarshalerBytes)
+ if err != nil || !bytes.Equal(got, want) {
+ t.Fatalf("proto.Marshal(%v) = %v, %v; want %v, nil", m, got, err, want)
+ }
+}