From b028a76c61b7288aefe6746ab7b561d7eb15ab71 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Wed, 14 Mar 2018 15:21:10 -0700 Subject: [PATCH] proto: ignore unknown fields in map entries (#561) A proto map entry is conceptually a message with two fields. In the same way that unknown fields don't cause a parsing error, map parsing should ignore unknown fields and tags. Similarly, it treats a missing field as the zero value. Fixes #403 --- proto/map_test.go | 24 +++++ proto/proto3_proto/proto3.pb.go | 124 ++++++++++++++----------- proto/proto3_proto/proto3.proto | 2 + proto/table_unmarshal.go | 159 ++++++++++++++------------------ 4 files changed, 163 insertions(+), 146 deletions(-) diff --git a/proto/map_test.go b/proto/map_test.go index 313e879245..b1e1529ee3 100644 --- a/proto/map_test.go +++ b/proto/map_test.go @@ -2,12 +2,36 @@ package proto_test import ( "fmt" + "reflect" "testing" "github.com/golang/protobuf/proto" ppb "github.com/golang/protobuf/proto/proto3_proto" ) +func TestMap(t *testing.T) { + var b []byte + fmt.Sscanf("a2010c0a044b657931120456616c31a201130a044b657932120556616c3261120456616c32a201240a044b6579330d05000000120556616c33621a0556616c3361120456616c331505000000a20100a201260a044b657934130a07536f6d6555524c1209536f6d655469746c651a08536e69707065743114", "%x", &b) + + var m ppb.Message + if err := proto.Unmarshal(b, &m); err != nil { + t.Fatalf("proto.Unmarshal error: %v", err) + } + + got := m.StringMap + want := map[string]string{ + "": "", + "Key1": "Val1", + "Key2": "Val2", + "Key3": "Val3", + "Key4": "", + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("maps differ:\ngot %#v\nwant %#v", got, want) + } +} + func marshalled() []byte { m := &ppb.IntMaps{} for i := 0; i < 1000; i++ { diff --git a/proto/proto3_proto/proto3.pb.go b/proto/proto3_proto/proto3.pb.go index b7a09b9939..a80f08930a 100644 --- a/proto/proto3_proto/proto3.pb.go +++ b/proto/proto3_proto/proto3.pb.go @@ -46,7 +46,7 @@ func (x Message_Humour) String() string { return proto.EnumName(Message_Humour_name, int32(x)) } func (Message_Humour) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_proto3_ef797ec3431c4aaa, []int{0, 0} + return fileDescriptor_proto3_e706e4ff19a5dbea, []int{0, 0} } type Message struct { @@ -68,6 +68,7 @@ type Message struct { ManyThings []*any.Any `protobuf:"bytes,15,rep,name=many_things,json=manyThings" json:"many_things,omitempty"` Submessage *Message `protobuf:"bytes,17,opt,name=submessage" json:"submessage,omitempty"` Children []*Message `protobuf:"bytes,18,rep,name=children" json:"children,omitempty"` + StringMap map[string]string `protobuf:"bytes,20,rep,name=string_map,json=stringMap" json:"string_map,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -77,7 +78,7 @@ func (m *Message) Reset() { *m = Message{} } func (m *Message) String() string { return proto.CompactTextString(m) } func (*Message) ProtoMessage() {} func (*Message) Descriptor() ([]byte, []int) { - return fileDescriptor_proto3_ef797ec3431c4aaa, []int{0} + return fileDescriptor_proto3_e706e4ff19a5dbea, []int{0} } func (m *Message) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Message.Unmarshal(m, b) @@ -223,6 +224,13 @@ func (m *Message) GetChildren() []*Message { return nil } +func (m *Message) GetStringMap() map[string]string { + if m != nil { + return m.StringMap + } + return nil +} + type Nested struct { Bunny string `protobuf:"bytes,1,opt,name=bunny" json:"bunny,omitempty"` Cute bool `protobuf:"varint,2,opt,name=cute" json:"cute,omitempty"` @@ -235,7 +243,7 @@ func (m *Nested) Reset() { *m = Nested{} } func (m *Nested) String() string { return proto.CompactTextString(m) } func (*Nested) ProtoMessage() {} func (*Nested) Descriptor() ([]byte, []int) { - return fileDescriptor_proto3_ef797ec3431c4aaa, []int{1} + return fileDescriptor_proto3_e706e4ff19a5dbea, []int{1} } func (m *Nested) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Nested.Unmarshal(m, b) @@ -280,7 +288,7 @@ func (m *MessageWithMap) Reset() { *m = MessageWithMap{} } func (m *MessageWithMap) String() string { return proto.CompactTextString(m) } func (*MessageWithMap) ProtoMessage() {} func (*MessageWithMap) Descriptor() ([]byte, []int) { - return fileDescriptor_proto3_ef797ec3431c4aaa, []int{2} + return fileDescriptor_proto3_e706e4ff19a5dbea, []int{2} } func (m *MessageWithMap) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_MessageWithMap.Unmarshal(m, b) @@ -318,7 +326,7 @@ func (m *IntMap) Reset() { *m = IntMap{} } func (m *IntMap) String() string { return proto.CompactTextString(m) } func (*IntMap) ProtoMessage() {} func (*IntMap) Descriptor() ([]byte, []int) { - return fileDescriptor_proto3_ef797ec3431c4aaa, []int{3} + return fileDescriptor_proto3_e706e4ff19a5dbea, []int{3} } func (m *IntMap) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_IntMap.Unmarshal(m, b) @@ -356,7 +364,7 @@ func (m *IntMaps) Reset() { *m = IntMaps{} } func (m *IntMaps) String() string { return proto.CompactTextString(m) } func (*IntMaps) ProtoMessage() {} func (*IntMaps) Descriptor() ([]byte, []int) { - return fileDescriptor_proto3_ef797ec3431c4aaa, []int{4} + return fileDescriptor_proto3_e706e4ff19a5dbea, []int{4} } func (m *IntMaps) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_IntMaps.Unmarshal(m, b) @@ -386,6 +394,7 @@ func (m *IntMaps) GetMaps() []*IntMap { func init() { proto.RegisterType((*Message)(nil), "proto3_proto.Message") proto.RegisterMapType((map[string]*test_proto.SubDefaults)(nil), "proto3_proto.Message.Proto2ValueEntry") + proto.RegisterMapType((map[string]string)(nil), "proto3_proto.Message.StringMapEntry") proto.RegisterMapType((map[string]*Nested)(nil), "proto3_proto.Message.TerrainEntry") proto.RegisterType((*Nested)(nil), "proto3_proto.Nested") proto.RegisterType((*MessageWithMap)(nil), "proto3_proto.MessageWithMap") @@ -396,54 +405,57 @@ func init() { proto.RegisterEnum("proto3_proto.Message_Humour", Message_Humour_name, Message_Humour_value) } -func init() { proto.RegisterFile("proto3_proto/proto3.proto", fileDescriptor_proto3_ef797ec3431c4aaa) } - -var fileDescriptor_proto3_ef797ec3431c4aaa = []byte{ - // 736 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x53, 0x5d, 0x6f, 0xda, 0x48, - 0x14, 0x5d, 0x63, 0x3e, 0xcc, 0xb5, 0x49, 0xbc, 0xb3, 0x44, 0x3b, 0x61, 0x77, 0x25, 0x2f, 0x2b, - 0xad, 0xac, 0xd5, 0xc6, 0xd9, 0x25, 0x4a, 0x15, 0x45, 0x55, 0xab, 0x24, 0x4d, 0x54, 0x14, 0x42, - 0xd1, 0x90, 0x34, 0xea, 0x93, 0x65, 0x60, 0x00, 0xab, 0x78, 0x4c, 0xed, 0x71, 0x25, 0x3f, 0xf7, - 0x9f, 0xf4, 0x97, 0x56, 0x9e, 0x31, 0xc4, 0x89, 0x9c, 0xf6, 0x89, 0x3b, 0x67, 0xce, 0xbd, 0xe7, - 0x70, 0x7c, 0x07, 0xf6, 0xd7, 0x51, 0xc8, 0xc3, 0x23, 0x57, 0xfc, 0x1c, 0xca, 0x83, 0x23, 0x7e, - 0x90, 0x51, 0xbc, 0xea, 0xec, 0x2f, 0xc2, 0x70, 0xb1, 0xa2, 0x92, 0x32, 0x49, 0xe6, 0x87, 0x1e, - 0x4b, 0x25, 0xb1, 0xb3, 0xc7, 0x69, 0xcc, 0xf3, 0x09, 0x59, 0x29, 0xe1, 0xee, 0x17, 0x0d, 0x1a, - 0x37, 0x34, 0x8e, 0xbd, 0x05, 0x45, 0x08, 0xaa, 0xcc, 0x0b, 0x28, 0x56, 0x2c, 0xc5, 0x6e, 0x12, - 0x51, 0xa3, 0x13, 0xd0, 0x96, 0xfe, 0xca, 0x8b, 0x7c, 0x9e, 0xe2, 0x8a, 0xa5, 0xd8, 0x3b, 0xbd, - 0xdf, 0x9d, 0xa2, 0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 0xc0, 0x5b, 0xc7, 0xb9, 0x68, 0xbb, 0x4c, 0x94, 0x08, 0xc6, 0xa4, - 0x2e, 0xaf, 0xbe, 0x05, 0x00, 0x00, 0xff, 0xff, 0x20, 0xb7, 0x04, 0xd9, 0xea, 0x05, 0x00, 0x00, +func init() { proto.RegisterFile("proto3_proto/proto3.proto", fileDescriptor_proto3_e706e4ff19a5dbea) } + +var fileDescriptor_proto3_e706e4ff19a5dbea = []byte{ + // 774 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x94, 0x6f, 0x8f, 0xdb, 0x44, + 0x10, 0xc6, 0x71, 0x9c, 0x3f, 0xce, 0xd8, 0x77, 0x35, 0x4b, 0x2a, 0xb6, 0x01, 0x24, 0x13, 0x10, + 0xb2, 0x10, 0xf5, 0x41, 0xaa, 0x43, 0x55, 0x55, 0x81, 0xee, 0x8e, 0x56, 0x44, 0x77, 0x17, 0xa2, + 0xcd, 0x95, 0x13, 0xaf, 0xac, 0x4d, 0x6e, 0x93, 0x58, 0xc4, 0xeb, 0xe0, 0x5d, 0x23, 0xf9, 0x0b, + 0xf0, 0x41, 0xf8, 0xa4, 0x68, 0x77, 0x9d, 0xd4, 0xa9, 0x5c, 0xfa, 0x2a, 0xbb, 0x8f, 0x7f, 0x33, + 0xcf, 0x78, 0x66, 0x1c, 0x78, 0xb2, 0xcb, 0x33, 0x99, 0x3d, 0x8b, 0xf5, 0xcf, 0x99, 0xb9, 0x44, + 0xfa, 0x07, 0x79, 0xf5, 0x47, 0xc3, 0x27, 0xeb, 0x2c, 0x5b, 0x6f, 0x99, 0x41, 0x16, 0xc5, 0xea, + 0x8c, 0xf2, 0xd2, 0x80, 0xc3, 0xc7, 0x92, 0x09, 0x59, 0x65, 0x50, 0x47, 0x23, 0x8f, 0xfe, 0xe9, + 0x43, 0xef, 0x96, 0x09, 0x41, 0xd7, 0x0c, 0x21, 0x68, 0x73, 0x9a, 0x32, 0x6c, 0x05, 0x56, 0xd8, + 0x27, 0xfa, 0x8c, 0x9e, 0x83, 0xb3, 0x49, 0xb6, 0x34, 0x4f, 0x64, 0x89, 0x5b, 0x81, 0x15, 0x9e, + 0x8e, 0x3f, 0x8f, 0xea, 0x96, 0x51, 0x15, 0x1c, 0xfd, 0x5a, 0xa4, 0x59, 0x91, 0x93, 0x03, 0x8d, + 0x02, 0xf0, 0x36, 0x2c, 0x59, 0x6f, 0x64, 0x9c, 0xf0, 0x78, 0x99, 0x62, 0x3b, 0xb0, 0xc2, 0x13, + 0x02, 0x46, 0x9b, 0xf0, 0xab, 0x54, 0xf9, 0x3d, 0x50, 0x49, 0x71, 0x3b, 0xb0, 0x42, 0x8f, 0xe8, + 0x33, 0xfa, 0x12, 0xbc, 0x9c, 0x89, 0x62, 0x2b, 0xe3, 0x65, 0x56, 0x70, 0x89, 0x7b, 0x81, 0x15, + 0xda, 0xc4, 0x35, 0xda, 0x95, 0x92, 0xd0, 0x57, 0x70, 0x22, 0xf3, 0x82, 0xc5, 0x62, 0x99, 0x49, + 0x91, 0x52, 0x8e, 0x9d, 0xc0, 0x0a, 0x1d, 0xe2, 0x29, 0x71, 0x5e, 0x69, 0x68, 0x00, 0x1d, 0xb1, + 0xcc, 0x72, 0x86, 0xfb, 0x81, 0x15, 0xb6, 0x88, 0xb9, 0x20, 0x1f, 0xec, 0x3f, 0x59, 0x89, 0x3b, + 0x81, 0x1d, 0xb6, 0x89, 0x3a, 0xa2, 0xcf, 0xa0, 0x2f, 0x36, 0x59, 0x2e, 0x63, 0xa5, 0x7f, 0x12, + 0xd8, 0x61, 0x87, 0x38, 0x5a, 0xb8, 0x66, 0x25, 0xfa, 0x0e, 0xba, 0x9c, 0x09, 0xc9, 0x1e, 0x70, + 0x37, 0xb0, 0x42, 0x77, 0x3c, 0x38, 0x7e, 0xf5, 0xa9, 0x7e, 0x46, 0x2a, 0x06, 0x9d, 0x43, 0x2f, + 0x8f, 0x57, 0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4a, 0x76, 0x6b, 0xf8, 0x6a, 0xb5, 0x17, + 0x6f, 0x95, 0xe1, 0x4f, 0xe0, 0xbf, 0x0b, 0xd4, 0x3b, 0xe3, 0x34, 0x74, 0xc6, 0xab, 0x77, 0xe6, + 0x2f, 0xe8, 0x4e, 0xb8, 0x54, 0xb5, 0x9d, 0x81, 0x9d, 0x4b, 0x59, 0x95, 0xf4, 0xc5, 0x71, 0x49, + 0x06, 0x89, 0x88, 0x94, 0xa6, 0x04, 0x45, 0x0e, 0x7f, 0x04, 0x67, 0x2f, 0xd4, 0x2d, 0x3b, 0x0d, + 0x96, 0x9d, 0xba, 0xe5, 0x33, 0xe8, 0x99, 0x7c, 0x02, 0x85, 0xd0, 0x4e, 0xe9, 0x4e, 0x54, 0xa6, + 0x83, 0x26, 0x53, 0xa2, 0x89, 0x45, 0xd7, 0x3c, 0xfa, 0x2f, 0x00, 0x00, 0xff, 0xff, 0x99, 0x24, + 0x6b, 0x12, 0x6d, 0x06, 0x00, 0x00, } diff --git a/proto/proto3_proto/proto3.proto b/proto/proto3_proto/proto3.proto index b927660f40..c81fe1e5e0 100644 --- a/proto/proto3_proto/proto3.proto +++ b/proto/proto3_proto/proto3.proto @@ -66,6 +66,8 @@ message Message { Message submessage = 17; repeated Message children = 18; + + map string_map = 20; } message Nested { diff --git a/proto/table_unmarshal.go b/proto/table_unmarshal.go index e832cbc212..8c0c10db51 100644 --- a/proto/table_unmarshal.go +++ b/proto/table_unmarshal.go @@ -188,40 +188,10 @@ func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error { // Unknown tag. if !u.unrecognized.IsValid() { // Don't keep unrecognized data; just skip it. - // Use wire type to skip data. - switch wire { - case WireVarint: - _, k := decodeVarint(b) - if k == 0 { - return io.ErrUnexpectedEOF - } - b = b[k:] - case WireFixed32: - if len(b) < 4 { - return io.ErrUnexpectedEOF - } - b = b[4:] - case WireFixed64: - if len(b) < 8 { - return io.ErrUnexpectedEOF - } - b = b[8:] - case WireBytes: - m, k := decodeVarint(b) - if k == 0 || uint64(len(b)-k) < m { - return io.ErrUnexpectedEOF - } - b = b[uint64(k)+m:] - case WireStartGroup: - // Proto3 doesn't have groups. But the data may have - // been encoded with proto2. - _, i := findEndGroup(b) - if i == -1 { - return io.ErrUnexpectedEOF - } - b = b[i:] - default: - return fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, u.typ) + var err error + b, err = skipField(b, wire) + if err != nil { + return err } continue } @@ -253,49 +223,17 @@ func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error { panic("no extensions field available") } } + // Use wire type to skip data. - switch wire { - case WireVarint: - _, k := decodeVarint(b) - if k == 0 { - return io.ErrUnexpectedEOF - } - *z = encodeVarint(*z, tag<<3|uint64(wire)) - *z = append(*z, b[:k]...) - b = b[k:] - case WireFixed32: - if len(b) < 4 { - return io.ErrUnexpectedEOF - } - *z = encodeVarint(*z, tag<<3|uint64(wire)) - *z = append(*z, b[:4]...) - b = b[4:] - case WireFixed64: - if len(b) < 8 { - return io.ErrUnexpectedEOF - } - *z = encodeVarint(*z, tag<<3|uint64(wire)) - *z = append(*z, b[:8]...) - b = b[8:] - case WireBytes: - m, k := decodeVarint(b) - if k == 0 || uint64(len(b)) < uint64(k)+m { - return io.ErrUnexpectedEOF - } - *z = encodeVarint(*z, tag<<3|uint64(wire)) - *z = append(*z, b[:uint64(k)+m]...) - b = b[uint64(k)+m:] - case WireStartGroup: - _, i := findEndGroup(b) - if i == -1 { - return io.ErrUnexpectedEOF - } - *z = encodeVarint(*z, tag<<3|uint64(wire)) - *z = append(*z, b[:i]...) - b = b[i:] - default: - return fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, u.typ) + var err error + b0 := b + b, err = skipField(b, wire) + if err != nil { + return err } + *z = encodeVarint(*z, tag<<3|uint64(wire)) + *z = append(*z, b0[:len(b0)-len(b)]...) + if emap != nil { emap[int32(tag)] = e } @@ -1742,26 +1680,30 @@ func makeUnmarshalMap(f *reflect.StructField) unmarshaler { if n == 0 { return nil, io.ErrUnexpectedEOF } + wire := int(x) & 7 b = b[n:] + + var err error switch x >> 3 { case 1: - var err error - b, err = unmarshalKey(b, valToPointer(k), int(x)&7) - if err != nil { - return nil, err - } + b, err = unmarshalKey(b, valToPointer(k), wire) case 2: - var err error - b, err = unmarshalVal(b, valToPointer(v), int(x)&7) - if err != nil { - return nil, err - } + b, err = unmarshalVal(b, valToPointer(v), wire) default: - // Unknown tag. Technically we could skip this - // entry and be ok. But it's much more likely - // to be error somewhere - map entries shouldn't - // have additional tags. - return nil, io.ErrUnexpectedEOF + err = errInternalBadWireType // skip unknown tag + } + + if err == nil { + continue + } + if err != errInternalBadWireType { + return nil, err + } + + // Skip past unknown fields. + b, err = skipField(b, wire) + if err != nil { + return nil, err } } @@ -1815,6 +1757,43 @@ func makeUnmarshalOneof(typ, ityp reflect.Type, unmarshal unmarshaler) unmarshal // Error used by decode internally. var errInternalBadWireType = errors.New("proto: internal error: bad wiretype") +// skipField skips past a field of type wire and returns the remaining bytes. +func skipField(b []byte, wire int) ([]byte, error) { + switch wire { + case WireVarint: + _, k := decodeVarint(b) + if k == 0 { + return b, io.ErrUnexpectedEOF + } + b = b[k:] + case WireFixed32: + if len(b) < 4 { + return b, io.ErrUnexpectedEOF + } + b = b[4:] + case WireFixed64: + if len(b) < 8 { + return b, io.ErrUnexpectedEOF + } + b = b[8:] + case WireBytes: + m, k := decodeVarint(b) + if k == 0 || uint64(len(b)-k) < m { + return b, io.ErrUnexpectedEOF + } + b = b[uint64(k)+m:] + case WireStartGroup: + _, i := findEndGroup(b) + if i == -1 { + return b, io.ErrUnexpectedEOF + } + b = b[i:] + default: + return b, fmt.Errorf("proto: can't skip unknown wire type %d", wire) + } + return b, nil +} + // findEndGroup finds the index of the next EndGroup tag. // Groups may be nested, so the "next" EndGroup tag is the first // unpaired EndGroup.