fix(codec): MarshalInterface should err when UnmarshalInterface will fail (#12964)

## Description

Closes: #12894 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Matt Kocubinski
2022-08-19 18:42:55 +00:00
committed by GitHub
parent f430528ebc
commit 28a7e33bc9
13 changed files with 1009 additions and 118 deletions
+11 -1
View File
@@ -17,10 +17,20 @@ type Animal interface {
Greet() string
}
func (c Cat) Greet() string {
type Cartoon interface {
proto.Message
Identify() string
}
func (c *Cat) Greet() string {
return fmt.Sprintf("Meow, my name is %s", c.Moniker)
}
func (c *Bird) Identify() string {
return "This is Tweety."
}
func (d Dog) Greet() string {
return fmt.Sprintf("Roof, my name is %s", d.Name)
}
+239 -33
View File
@@ -128,6 +128,58 @@ func (m *Cat) GetLives() int32 {
return 0
}
type Bird struct {
Species string `protobuf:"bytes,1,opt,name=species,proto3" json:"species,omitempty"`
Color int32 `protobuf:"varint,2,opt,name=color,proto3" json:"color,omitempty"`
}
func (m *Bird) Reset() { *m = Bird{} }
func (m *Bird) String() string { return proto.CompactTextString(m) }
func (*Bird) ProtoMessage() {}
func (*Bird) Descriptor() ([]byte, []int) {
return fileDescriptor_40c4782d007dfce9, []int{2}
}
func (m *Bird) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *Bird) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_Bird.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *Bird) XXX_Merge(src proto.Message) {
xxx_messageInfo_Bird.Merge(m, src)
}
func (m *Bird) XXX_Size() int {
return m.Size()
}
func (m *Bird) XXX_DiscardUnknown() {
xxx_messageInfo_Bird.DiscardUnknown(m)
}
var xxx_messageInfo_Bird proto.InternalMessageInfo
func (m *Bird) GetSpecies() string {
if m != nil {
return m.Species
}
return ""
}
func (m *Bird) GetColor() int32 {
if m != nil {
return m.Color
}
return 0
}
type HasAnimal struct {
Animal *types.Any `protobuf:"bytes,1,opt,name=animal,proto3" json:"animal,omitempty"`
X int64 `protobuf:"varint,2,opt,name=x,proto3" json:"x,omitempty"`
@@ -137,7 +189,7 @@ func (m *HasAnimal) Reset() { *m = HasAnimal{} }
func (m *HasAnimal) String() string { return proto.CompactTextString(m) }
func (*HasAnimal) ProtoMessage() {}
func (*HasAnimal) Descriptor() ([]byte, []int) {
return fileDescriptor_40c4782d007dfce9, []int{2}
return fileDescriptor_40c4782d007dfce9, []int{3}
}
func (m *HasAnimal) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
@@ -188,7 +240,7 @@ func (m *HasHasAnimal) Reset() { *m = HasHasAnimal{} }
func (m *HasHasAnimal) String() string { return proto.CompactTextString(m) }
func (*HasHasAnimal) ProtoMessage() {}
func (*HasHasAnimal) Descriptor() ([]byte, []int) {
return fileDescriptor_40c4782d007dfce9, []int{3}
return fileDescriptor_40c4782d007dfce9, []int{4}
}
func (m *HasHasAnimal) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
@@ -232,7 +284,7 @@ func (m *HasHasHasAnimal) Reset() { *m = HasHasHasAnimal{} }
func (m *HasHasHasAnimal) String() string { return proto.CompactTextString(m) }
func (*HasHasHasAnimal) ProtoMessage() {}
func (*HasHasHasAnimal) Descriptor() ([]byte, []int) {
return fileDescriptor_40c4782d007dfce9, []int{4}
return fileDescriptor_40c4782d007dfce9, []int{5}
}
func (m *HasHasHasAnimal) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
@@ -279,7 +331,7 @@ func (m *BadMultiSignature) Reset() { *m = BadMultiSignature{} }
func (m *BadMultiSignature) String() string { return proto.CompactTextString(m) }
func (*BadMultiSignature) ProtoMessage() {}
func (*BadMultiSignature) Descriptor() ([]byte, []int) {
return fileDescriptor_40c4782d007dfce9, []int{5}
return fileDescriptor_40c4782d007dfce9, []int{6}
}
func (m *BadMultiSignature) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
@@ -333,7 +385,7 @@ func (m *TableModel) Reset() { *m = TableModel{} }
func (m *TableModel) String() string { return proto.CompactTextString(m) }
func (*TableModel) ProtoMessage() {}
func (*TableModel) Descriptor() ([]byte, []int) {
return fileDescriptor_40c4782d007dfce9, []int{6}
return fileDescriptor_40c4782d007dfce9, []int{7}
}
func (m *TableModel) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
@@ -393,6 +445,7 @@ func (m *TableModel) GetMetadata() []byte {
func init() {
proto.RegisterType((*Dog)(nil), "testdata.Dog")
proto.RegisterType((*Cat)(nil), "testdata.Cat")
proto.RegisterType((*Bird)(nil), "testdata.Bird")
proto.RegisterType((*HasAnimal)(nil), "testdata.HasAnimal")
proto.RegisterType((*HasHasAnimal)(nil), "testdata.HasHasAnimal")
proto.RegisterType((*HasHasHasAnimal)(nil), "testdata.HasHasHasAnimal")
@@ -403,34 +456,35 @@ func init() {
func init() { proto.RegisterFile("testdata.proto", fileDescriptor_40c4782d007dfce9) }
var fileDescriptor_40c4782d007dfce9 = []byte{
// 419 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0xb1, 0x6e, 0xdb, 0x30,
0x10, 0x35, 0x2d, 0xd9, 0x8d, 0xaf, 0x82, 0x82, 0x12, 0x46, 0xa1, 0x7a, 0x50, 0x0d, 0x2d, 0xf5,
0xd0, 0x48, 0x40, 0x83, 0x2e, 0xd9, 0x92, 0x14, 0xad, 0x17, 0x2f, 0x6a, 0xa7, 0x2e, 0x01, 0x65,
0x32, 0x12, 0x11, 0x52, 0x2c, 0x44, 0xaa, 0x48, 0xfa, 0x15, 0xfd, 0x85, 0xfe, 0x4d, 0x47, 0x8f,
0x1d, 0x0b, 0xfb, 0x47, 0x0a, 0x51, 0x92, 0xed, 0xa1, 0x83, 0x27, 0xbe, 0xf7, 0xee, 0xde, 0xe3,
0x81, 0x3c, 0xf0, 0x0d, 0xd3, 0x86, 0x12, 0x43, 0xe2, 0x6f, 0x95, 0x32, 0x0a, 0x9f, 0xf5, 0x7c,
0x36, 0xcd, 0x55, 0xae, 0xac, 0x98, 0x34, 0xa8, 0xad, 0xcf, 0x5e, 0xe5, 0x4a, 0xe5, 0x82, 0x25,
0x96, 0x65, 0xf5, 0x7d, 0x42, 0xca, 0xa7, 0xb6, 0x14, 0x5d, 0x80, 0xf3, 0x41, 0xe5, 0x18, 0x83,
0xab, 0xf9, 0x0f, 0x16, 0xa0, 0x39, 0x5a, 0x4c, 0x52, 0x8b, 0x1b, 0xad, 0x24, 0x92, 0x05, 0xc3,
0x56, 0x6b, 0x70, 0xf4, 0x1e, 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b1, 0x2a, 0x70, 0x6c,
0x5f, 0xc7, 0xf0, 0x0c, 0xce, 0x24, 0x33, 0xa4, 0xd9, 0x96, 0xc0, 0xb5, 0x37, 0xee, 0xf9, 0xcd,
0xf2, 0xf7, 0x36, 0x44, 0x9b, 0x6d, 0x88, 0xfe, 0x6e, 0x43, 0xf4, 0x73, 0x17, 0x0e, 0x36, 0xbb,
0x70, 0xf0, 0x67, 0x17, 0x0e, 0xbe, 0xc6, 0x39, 0x37, 0x45, 0x9d, 0xc5, 0x6b, 0x25, 0x93, 0xb5,
0xd2, 0x52, 0xe9, 0xee, 0xb8, 0xd0, 0xf4, 0x21, 0x69, 0xd6, 0xaf, 0x36, 0x5c, 0x24, 0xfd, 0x1e,
0x66, 0x63, 0xfb, 0x5e, 0x97, 0xff, 0x02, 0x00, 0x00, 0xff, 0xff, 0xb5, 0x8f, 0xef, 0x9c, 0xaa,
0x02, 0x00, 0x00,
// 443 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xc1, 0x6e, 0xd3, 0x30,
0x1c, 0xc6, 0xeb, 0x26, 0x2d, 0xeb, 0x9f, 0xa8, 0x13, 0x56, 0x85, 0x42, 0x0f, 0x61, 0xca, 0x85,
0x1e, 0x58, 0x22, 0x31, 0xc1, 0x61, 0xb7, 0x75, 0x08, 0x7a, 0xe9, 0x25, 0x70, 0xe2, 0x32, 0x39,
0xb1, 0x97, 0x5a, 0x73, 0xe2, 0x29, 0x76, 0xd0, 0xc6, 0x53, 0xf0, 0x0a, 0xbc, 0x0d, 0xc7, 0x1e,
0x39, 0xa2, 0xf6, 0x45, 0x90, 0x9d, 0x78, 0xdb, 0x81, 0x43, 0x4f, 0xf9, 0xbe, 0xcf, 0xfe, 0x7d,
0x89, 0x9d, 0x3f, 0x4c, 0x35, 0x53, 0x9a, 0x12, 0x4d, 0x92, 0xdb, 0x46, 0x6a, 0x89, 0x8f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 0xff, 0xc2, 0xcb, 0x9a, 0xe8, 0xb6, 0x61, 0x38, 0x02,
0x50, 0xce, 0x98, 0x4b, 0xf2, 0x16, 0x41, 0xf6, 0x24, 0xc1, 0x6f, 0xe0, 0xb8, 0x22, 0x82, 0x17,
0x5c, 0xb6, 0xea, 0xea, 0x9a, 0x33, 0x41, 0xc3, 0xd1, 0x09, 0x5a, 0x04, 0xd9, 0xf4, 0x21, 0xfe,
0x64, 0xd2, 0x73, 0x7f, 0xfb, 0xeb, 0x35, 0x8a, 0x29, 0xc0, 0x57, 0x92, 0x0b, 0xb6, 0x96, 0x94,
0x09, 0x3c, 0x85, 0x21, 0xa7, 0xf6, 0x0b, 0xfd, 0x6c, 0xc8, 0xe9, 0xff, 0xfe, 0x30, 0x7e, 0x09,
0xe3, 0xba, 0xad, 0x72, 0xd6, 0x84, 0x9e, 0xdd, 0xd7, 0x3b, 0x3c, 0x87, 0xa3, 0x8a, 0x69, 0x62,
0xa6, 0x2c, 0xf4, 0xed, 0x1b, 0x1f, 0xfc, 0x72, 0xf5, 0x7b, 0x17, 0xa1, 0xed, 0x2e, 0x42, 0x7f,
0x77, 0x11, 0xfa, 0xb9, 0x8f, 0x06, 0xdb, 0x7d, 0x34, 0xf8, 0xb3, 0x8f, 0x06, 0xdf, 0x92, 0x92,
0xeb, 0x4d, 0x9b, 0x27, 0x85, 0xac, 0xd2, 0x42, 0xaa, 0x4a, 0xaa, 0xfe, 0x71, 0xaa, 0xe8, 0x4d,
0x6a, 0xc6, 0xb6, 0xd5, 0x5c, 0xa4, 0x6e, 0x7e, 0xf3, 0xb1, 0xbd, 0xaf, 0xb3, 0x7f, 0x01, 0x00,
0x00, 0xff, 0xff, 0xbd, 0x48, 0xf9, 0x24, 0xe2, 0x02, 0x00, 0x00,
}
func (m *Dog) Marshal() (dAtA []byte, err error) {
@@ -505,6 +559,41 @@ func (m *Cat) MarshalToSizedBuffer(dAtA []byte) (int, error) {
return len(dAtA) - i, nil
}
func (m *Bird) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *Bird) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *Bird) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if m.Color != 0 {
i = encodeVarintTestdata(dAtA, i, uint64(m.Color))
i--
dAtA[i] = 0x10
}
if len(m.Species) > 0 {
i -= len(m.Species)
copy(dAtA[i:], m.Species)
i = encodeVarintTestdata(dAtA, i, uint64(len(m.Species)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func (m *HasAnimal) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
@@ -749,6 +838,22 @@ func (m *Cat) Size() (n int) {
return n
}
func (m *Bird) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Species)
if l > 0 {
n += 1 + l + sovTestdata(uint64(l))
}
if m.Color != 0 {
n += 1 + sovTestdata(uint64(m.Color))
}
return n
}
func (m *HasAnimal) Size() (n int) {
if m == nil {
return 0
@@ -1057,6 +1162,107 @@ func (m *Cat) Unmarshal(dAtA []byte) error {
}
return nil
}
func (m *Bird) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowTestdata
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: Bird: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: Bird: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Species", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowTestdata
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthTestdata
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthTestdata
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Species = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Color", wireType)
}
m.Color = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowTestdata
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.Color |= int32(b&0x7F) << shift
if b < 0x80 {
break
}
}
default:
iNdEx = preIndex
skippy, err := skipTestdata(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthTestdata
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *HasAnimal) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
+5
View File
@@ -16,6 +16,11 @@ message Cat {
int32 lives = 2;
}
message Bird {
string species = 1;
int32 color = 2;
}
message HasAnimal {
google.protobuf.Any animal = 1;
int64 x = 2;