From 09ab7735f7757c093f5b0a2285bff3998d684a61 Mon Sep 17 00:00:00 2001 From: jmarais Date: Fri, 11 Oct 2019 16:07:31 +0200 Subject: [PATCH] Issue619safer (#627) * tests. added test case for issue (#619). Compare marshaling between a proto.Buffer and proto.Marshal with different proto.Buffer sizes. * proto/buffer. added buffer cap adjustment before giving it to marshal. This allows us to marshal at the correct place when the buffer contains a larger backing buffer. (#619) * proto/buffer. remove unsafe/reflect slice cap adjustment and rather use a full slice expression * proto/buffer. encode. add issue620 tests here * proto/buffer encode. removed unnecessary slicing. (#619) * proto/buffer. err check previously ignored error * issue619. add test package to make regenerate list --- Makefile | 2 + proto/encode.go | 2 + proto/table_marshal.go | 4 +- test/issue620/Makefile | 31 ++++ test/issue620/issue620.pb.go | 229 +++++++++++++++++++++++++++ test/issue620/issue620.proto | 44 +++++ test/issue620/issue620_test.go | 119 ++++++++++++++ test/protobuffer/Makefile | 31 ++++ test/protobuffer/protobuffer.pb.go | 220 +++++++++++++++++++++++++ test/protobuffer/protobuffer.proto | 47 ++++++ test/protobuffer/protobuffer_test.go | 78 +++++++++ 11 files changed, 806 insertions(+), 1 deletion(-) mode change 100755 => 100644 Makefile create mode 100644 test/issue620/Makefile create mode 100644 test/issue620/issue620.pb.go create mode 100644 test/issue620/issue620.proto create mode 100644 test/issue620/issue620_test.go create mode 100644 test/protobuffer/Makefile create mode 100644 test/protobuffer/protobuffer.pb.go create mode 100644 test/protobuffer/protobuffer.proto create mode 100644 test/protobuffer/protobuffer_test.go diff --git a/Makefile b/Makefile old mode 100755 new mode 100644 index 19528c8f94..598f250b9b --- a/Makefile +++ b/Makefile @@ -137,6 +137,8 @@ regenerate: make -C test/issue503 regenerate make -C test/issue530 regenerate make -C test/issue617 regenerate + make -C test/issue620 regenerate + make -C test/protobuffer regenerate make -C test/issue630 regenerate make gofmt diff --git a/proto/encode.go b/proto/encode.go index 3abfed2cff..9581ccd304 100644 --- a/proto/encode.go +++ b/proto/encode.go @@ -189,6 +189,8 @@ type Marshaler interface { // prefixed by a varint-encoded length. func (p *Buffer) EncodeMessage(pb Message) error { siz := Size(pb) + sizVar := SizeVarint(uint64(siz)) + p.grow(siz + sizVar) p.EncodeVarint(uint64(siz)) return p.Marshal(pb) } diff --git a/proto/table_marshal.go b/proto/table_marshal.go index db9927a0c7..f8babdefab 100644 --- a/proto/table_marshal.go +++ b/proto/table_marshal.go @@ -2969,7 +2969,9 @@ func (p *Buffer) Marshal(pb Message) error { if m, ok := pb.(newMarshaler); ok { siz := m.XXX_Size() p.grow(siz) // make sure buf has enough capacity - p.buf, err = m.XXX_Marshal(p.buf, p.deterministic) + pp := p.buf[len(p.buf) : len(p.buf) : len(p.buf)+siz] + pp, err = m.XXX_Marshal(pp, p.deterministic) + p.buf = append(p.buf, pp...) return err } if m, ok := pb.(Marshaler); ok { diff --git a/test/issue620/Makefile b/test/issue620/Makefile new file mode 100644 index 0000000000..e5bb3f58b8 --- /dev/null +++ b/test/issue620/Makefile @@ -0,0 +1,31 @@ +# Protocol Buffers for Go with Gadgets +# +# Copyright (c) 2019, The GoGo Authors. All rights reserved. +# http://github.com/gogo/protobuf +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +regenerate: + go install github.com/gogo/protobuf/protoc-gen-gogo + protoc --proto_path=../../../../../:../../protobuf/:. --gogo_out=. issue620.proto diff --git a/test/issue620/issue620.pb.go b/test/issue620/issue620.pb.go new file mode 100644 index 0000000000..092424d8e5 --- /dev/null +++ b/test/issue620/issue620.pb.go @@ -0,0 +1,229 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: issue620.proto + +package issue620 + +import ( + bytes "bytes" + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type Bar struct { + Field1 *string `protobuf:"bytes,1,opt,name=Field1" json:"Field1,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Bar) Reset() { *m = Bar{} } +func (m *Bar) String() string { return proto.CompactTextString(m) } +func (*Bar) ProtoMessage() {} +func (*Bar) Descriptor() ([]byte, []int) { + return fileDescriptor_63df3af46ac96f83, []int{0} +} +func (m *Bar) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Bar.Unmarshal(m, b) +} +func (m *Bar) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Bar.Marshal(b, m, deterministic) +} +func (m *Bar) XXX_Merge(src proto.Message) { + xxx_messageInfo_Bar.Merge(m, src) +} +func (m *Bar) XXX_Size() int { + return xxx_messageInfo_Bar.Size(m) +} +func (m *Bar) XXX_DiscardUnknown() { + xxx_messageInfo_Bar.DiscardUnknown(m) +} + +var xxx_messageInfo_Bar proto.InternalMessageInfo + +func (m *Bar) GetField1() string { + if m != nil && m.Field1 != nil { + return *m.Field1 + } + return "" +} + +type BarFast struct { + Field1 *string `protobuf:"bytes,1,opt,name=Field1" json:"Field1,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *BarFast) Reset() { *m = BarFast{} } +func (m *BarFast) String() string { return proto.CompactTextString(m) } +func (*BarFast) ProtoMessage() {} +func (*BarFast) Descriptor() ([]byte, []int) { + return fileDescriptor_63df3af46ac96f83, []int{1} +} +func (m *BarFast) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_BarFast.Unmarshal(m, b) +} +func (m *BarFast) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_BarFast.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 *BarFast) XXX_Merge(src proto.Message) { + xxx_messageInfo_BarFast.Merge(m, src) +} +func (m *BarFast) XXX_Size() int { + return m.Size() +} +func (m *BarFast) XXX_DiscardUnknown() { + xxx_messageInfo_BarFast.DiscardUnknown(m) +} + +var xxx_messageInfo_BarFast proto.InternalMessageInfo + +func (m *BarFast) GetField1() string { + if m != nil && m.Field1 != nil { + return *m.Field1 + } + return "" +} + +func init() { + proto.RegisterType((*Bar)(nil), "issue620.Bar") + proto.RegisterType((*BarFast)(nil), "issue620.BarFast") +} + +func init() { proto.RegisterFile("issue620.proto", fileDescriptor_63df3af46ac96f83) } + +var fileDescriptor_63df3af46ac96f83 = []byte{ + // 128 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xcb, 0x2c, 0x2e, 0x2e, + 0x4d, 0x35, 0x33, 0x32, 0xd0, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x80, 0xf1, 0xa5, 0x74, + 0xd3, 0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0xd3, 0xf3, 0xd3, 0xf3, 0xf5, + 0xc1, 0x0a, 0x92, 0x4a, 0xd3, 0xc0, 0x3c, 0x30, 0x07, 0xcc, 0x82, 0x68, 0x54, 0x92, 0xe5, 0x62, + 0x76, 0x4a, 0x2c, 0x12, 0x12, 0xe3, 0x62, 0x73, 0xcb, 0x4c, 0xcd, 0x49, 0x31, 0x94, 0x60, 0x54, + 0x60, 0xd4, 0xe0, 0x0c, 0x82, 0xf2, 0x94, 0xf4, 0xb9, 0xd8, 0x9d, 0x12, 0x8b, 0xdc, 0x12, 0x8b, + 0x4b, 0x70, 0x29, 0xb1, 0xe2, 0x79, 0xb1, 0x40, 0x9e, 0xb1, 0x63, 0xa1, 0x3c, 0xe3, 0x82, 0x85, + 0xf2, 0x8c, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x36, 0x1c, 0x6d, 0xdf, 0x99, 0x00, 0x00, 0x00, +} + +func (this *BarFast) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*BarFast) + if !ok { + that2, ok := that.(BarFast) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.Field1 != nil && that1.Field1 != nil { + if *this.Field1 != *that1.Field1 { + return false + } + } else if this.Field1 != nil { + return false + } else if that1.Field1 != nil { + return false + } + if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { + return false + } + return true +} +func (m *BarFast) 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 *BarFast) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *BarFast) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + if m.Field1 != nil { + i -= len(*m.Field1) + copy(dAtA[i:], *m.Field1) + i = encodeVarintIssue620(dAtA, i, uint64(len(*m.Field1))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintIssue620(dAtA []byte, offset int, v uint64) int { + offset -= sovIssue620(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *BarFast) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Field1 != nil { + l = len(*m.Field1) + n += 1 + l + sovIssue620(uint64(l)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func sovIssue620(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozIssue620(x uint64) (n int) { + return sovIssue620(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} diff --git a/test/issue620/issue620.proto b/test/issue620/issue620.proto new file mode 100644 index 0000000000..e242d2d0ef --- /dev/null +++ b/test/issue620/issue620.proto @@ -0,0 +1,44 @@ +// Protocol Buffers for Go with Gadgets +// +// Copyright (c) 2019, The GoGo Authors. All rights reserved. +// http://github.com/gogo/protobuf +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto2"; + +package issue620; + +import "github.com/gogo/protobuf/gogoproto/gogo.proto"; + +message Bar { + optional string Field1 = 1; +} + +message BarFast { + option (gogoproto.marshaler) = true; + option (gogoproto.sizer) = true; + option (gogoproto.equal) = true; + optional string Field1 = 1; +} diff --git a/test/issue620/issue620_test.go b/test/issue620/issue620_test.go new file mode 100644 index 0000000000..88cef18e5e --- /dev/null +++ b/test/issue620/issue620_test.go @@ -0,0 +1,119 @@ +// Protocol Buffers for Go with Gadgets +// +// Copyright (c) 2019, The GoGo Authors. All rights reserved. +// http://github.com/gogo/protobuf +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package issue620 + +import ( + "bytes" + "testing" + + "github.com/gogo/protobuf/proto" +) + +func TestEncodeShort(t *testing.T) { + exp := []byte{8, 10, 6, 66, 97, 114, 49, 50, 51} + s := "Bar123" + b1 := &Bar{Field1: &s} + bufProto := proto.NewBuffer(nil) + + b2 := &BarFast{Field1: &s} + bufProtoFast := proto.NewBuffer(nil) + + encodeMessageCheck(t, b1, b2, bufProto, bufProtoFast, exp) +} + +func TestEncodeLong(t *testing.T) { + exp := []byte{8, 10, 6, 66, 97, 114, 49, 50, 51} + s := "Bar123" + b1 := &Bar{Field1: &s} + bufProto := proto.NewBuffer(make([]byte, 0, 480)) + b2 := &BarFast{Field1: &s} + bufProtoFast := proto.NewBuffer(make([]byte, 0, 480)) + + encodeMessageCheck(t, b1, b2, bufProto, bufProtoFast, exp) +} + +func TestEncodeDecode(t *testing.T) { + s := "Bar123" + bar := &BarFast{Field1: &s} + bufProtoFast := proto.NewBuffer(make([]byte, 0, 480)) + err := bufProtoFast.EncodeMessage(bar) + errCheck(t, err) + dec := &BarFast{} + err = bufProtoFast.DecodeMessage(dec) + errCheck(t, err) + if !dec.Equal(bar) { + t.Errorf("Expect %+v got %+v after encode/decode", bar, dec) + } +} + +func encodeMessageCheck(t *testing.T, b1, b2 proto.Message, bufProto, bufProtoFast *proto.Buffer, exp []byte) { + err := bufProto.EncodeMessage(b1) + errCheck(t, err) + err = bufProtoFast.EncodeMessage(b2) + errCheck(t, err) + checkEqual(t, exp, bufProto.Bytes()) + checkEqual(t, exp, bufProtoFast.Bytes()) + + bufProto.Reset() + bufProtoFast.Reset() + expMore := make([]byte, 0, len(exp)) + copy(expMore, exp) + for i := 0; i < 10; i++ { + err = bufProto.EncodeMessage(b1) + errCheck(t, err) + err = bufProtoFast.EncodeMessage(b2) + errCheck(t, err) + expMore = append(expMore, exp...) + checkEqual(t, expMore, bufProto.Bytes()) + checkEqual(t, expMore, bufProtoFast.Bytes()) + } + + bufProto.Reset() + bufProtoFast.Reset() + err = bufProto.EncodeMessage(b1) + errCheck(t, err) + err = bufProtoFast.EncodeMessage(b2) + errCheck(t, err) + checkEqual(t, exp, bufProto.Bytes()) + checkEqual(t, exp, bufProtoFast.Bytes()) +} + +func errCheck(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Error(err.Error()) + } +} + +func checkEqual(t *testing.T, b1, b2 []byte) { + t.Helper() + if !bytes.Equal(b1, b2) { + t.Errorf("%v != %v\n", b1, b2) + } +} diff --git a/test/protobuffer/Makefile b/test/protobuffer/Makefile new file mode 100644 index 0000000000..00e39f73f4 --- /dev/null +++ b/test/protobuffer/Makefile @@ -0,0 +1,31 @@ +# Protocol Buffers for Go with Gadgets +# +# Copyright (c) 2019, The GoGo Authors. All rights reserved. +# http://github.com/gogo/protobuf +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +regenerate: + go install github.com/gogo/protobuf/protoc-gen-gogo + protoc --gogo_out=. --proto_path=../../../../../:../../protobuf/:. *.proto diff --git a/test/protobuffer/protobuffer.pb.go b/test/protobuffer/protobuffer.pb.go new file mode 100644 index 0000000000..ae8df3a673 --- /dev/null +++ b/test/protobuffer/protobuffer.pb.go @@ -0,0 +1,220 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: protobuffer.proto + +package protobuffer + +import ( + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type PBuffMarshal struct { + Field1 []byte `protobuf:"bytes,1,opt,name=Field1" json:"Field1,omitempty"` + Field2 *int32 `protobuf:"varint,2,opt,name=Field2" json:"Field2,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *PBuffMarshal) Reset() { *m = PBuffMarshal{} } +func (m *PBuffMarshal) String() string { return proto.CompactTextString(m) } +func (*PBuffMarshal) ProtoMessage() {} +func (*PBuffMarshal) Descriptor() ([]byte, []int) { + return fileDescriptor_12798c2215df1728, []int{0} +} +func (m *PBuffMarshal) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_PBuffMarshal.Unmarshal(m, b) +} +func (m *PBuffMarshal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_PBuffMarshal.Marshal(b, m, deterministic) +} +func (m *PBuffMarshal) XXX_Merge(src proto.Message) { + xxx_messageInfo_PBuffMarshal.Merge(m, src) +} +func (m *PBuffMarshal) XXX_Size() int { + return xxx_messageInfo_PBuffMarshal.Size(m) +} +func (m *PBuffMarshal) XXX_DiscardUnknown() { + xxx_messageInfo_PBuffMarshal.DiscardUnknown(m) +} + +var xxx_messageInfo_PBuffMarshal proto.InternalMessageInfo + +func (m *PBuffMarshal) GetField1() []byte { + if m != nil { + return m.Field1 + } + return nil +} + +func (m *PBuffMarshal) GetField2() int32 { + if m != nil && m.Field2 != nil { + return *m.Field2 + } + return 0 +} + +type PBuffMarshaler struct { + Field1 []byte `protobuf:"bytes,1,opt,name=Field1" json:"Field1,omitempty"` + Field2 *int32 `protobuf:"varint,2,opt,name=Field2" json:"Field2,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *PBuffMarshaler) Reset() { *m = PBuffMarshaler{} } +func (m *PBuffMarshaler) String() string { return proto.CompactTextString(m) } +func (*PBuffMarshaler) ProtoMessage() {} +func (*PBuffMarshaler) Descriptor() ([]byte, []int) { + return fileDescriptor_12798c2215df1728, []int{1} +} +func (m *PBuffMarshaler) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_PBuffMarshaler.Unmarshal(m, b) +} +func (m *PBuffMarshaler) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PBuffMarshaler.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 *PBuffMarshaler) XXX_Merge(src proto.Message) { + xxx_messageInfo_PBuffMarshaler.Merge(m, src) +} +func (m *PBuffMarshaler) XXX_Size() int { + return m.Size() +} +func (m *PBuffMarshaler) XXX_DiscardUnknown() { + xxx_messageInfo_PBuffMarshaler.DiscardUnknown(m) +} + +var xxx_messageInfo_PBuffMarshaler proto.InternalMessageInfo + +func (m *PBuffMarshaler) GetField1() []byte { + if m != nil { + return m.Field1 + } + return nil +} + +func (m *PBuffMarshaler) GetField2() int32 { + if m != nil && m.Field2 != nil { + return *m.Field2 + } + return 0 +} + +func init() { + proto.RegisterType((*PBuffMarshal)(nil), "protobuffer.PBuffMarshal") + proto.RegisterType((*PBuffMarshaler)(nil), "protobuffer.PBuffMarshaler") +} + +func init() { proto.RegisterFile("protobuffer.proto", fileDescriptor_12798c2215df1728) } + +var fileDescriptor_12798c2215df1728 = []byte{ + // 144 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0x28, 0xca, 0x2f, + 0xc9, 0x4f, 0x2a, 0x4d, 0x4b, 0x4b, 0x2d, 0xd2, 0x03, 0xb3, 0x85, 0xb8, 0x91, 0x84, 0xa4, 0x74, + 0xd3, 0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0xd3, 0xf3, 0xd3, 0xf3, 0xf5, + 0x61, 0x92, 0x60, 0x1e, 0x98, 0x03, 0x66, 0x41, 0xf4, 0x2a, 0x79, 0x70, 0xf1, 0x04, 0x38, 0x95, + 0xa6, 0xa5, 0xf9, 0x26, 0x16, 0x15, 0x67, 0x24, 0xe6, 0x08, 0x89, 0x71, 0xb1, 0xb9, 0x65, 0xa6, + 0xe6, 0xa4, 0x18, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x04, 0x41, 0x79, 0x70, 0x71, 0x23, 0x09, + 0x26, 0x05, 0x46, 0x0d, 0x56, 0xa8, 0xb8, 0x91, 0x15, 0x47, 0xc7, 0x42, 0x79, 0x86, 0x05, 0x0b, + 0xe5, 0x19, 0x94, 0xbc, 0xb8, 0xf8, 0x90, 0x4d, 0x4a, 0x2d, 0x22, 0xcb, 0x2c, 0xc6, 0x05, 0x0b, + 0xe5, 0x19, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xc0, 0x9d, 0x92, 0xe0, 0xe5, 0x00, 0x00, 0x00, +} + +func (m *PBuffMarshaler) 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 *PBuffMarshaler) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PBuffMarshaler) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + if m.Field2 != nil { + i = encodeVarintProtobuffer(dAtA, i, uint64(*m.Field2)) + i-- + dAtA[i] = 0x10 + } + if m.Field1 != nil { + i -= len(m.Field1) + copy(dAtA[i:], m.Field1) + i = encodeVarintProtobuffer(dAtA, i, uint64(len(m.Field1))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintProtobuffer(dAtA []byte, offset int, v uint64) int { + offset -= sovProtobuffer(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *PBuffMarshaler) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Field1 != nil { + l = len(m.Field1) + n += 1 + l + sovProtobuffer(uint64(l)) + } + if m.Field2 != nil { + n += 1 + sovProtobuffer(uint64(*m.Field2)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func sovProtobuffer(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozProtobuffer(x uint64) (n int) { + return sovProtobuffer(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} diff --git a/test/protobuffer/protobuffer.proto b/test/protobuffer/protobuffer.proto new file mode 100644 index 0000000000..43743cda07 --- /dev/null +++ b/test/protobuffer/protobuffer.proto @@ -0,0 +1,47 @@ +// Protocol Buffers for Go with Gadgets +// +// Copyright (c) 2019, The GoGo Authors. All rights reserved. +// http://github.com/gogo/protobuf +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto2"; + +package protobuffer; + +import "github.com/gogo/protobuf/gogoproto/gogo.proto"; + +message PBuffMarshal { + option (gogoproto.marshaler) = false; + option (gogoproto.sizer) = false; + optional bytes Field1 = 1; + optional int32 Field2 = 2; +} + +message PBuffMarshaler { + option (gogoproto.marshaler) = true; + option (gogoproto.sizer) = true; + optional bytes Field1 = 1; + optional int32 Field2 = 2; +} diff --git a/test/protobuffer/protobuffer_test.go b/test/protobuffer/protobuffer_test.go new file mode 100644 index 0000000000..79fef6142a --- /dev/null +++ b/test/protobuffer/protobuffer_test.go @@ -0,0 +1,78 @@ +// Protocol Buffers for Go with Gadgets +// +// Copyright (c) 2019, The GoGo Authors. All rights reserved. +// http://github.com/gogo/protobuf +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package protobuffer + +import ( + "bytes" + "testing" + + proto "github.com/gogo/protobuf/proto" +) + +func TestProtoBufferMarshal12(t *testing.T) { + i := int32(33) + m := &PBuffMarshal{Field1: []byte(string("Tester123")), Field2: &i} + mer := &PBuffMarshaler{Field1: []byte(string("Tester123")), Field2: &i} + testCases := []struct { + name string + size int + m proto.Message + }{ + {"MarshalLarge", 1024, m}, + {"MarshalSmall", 0, m}, + {"MarshalerLarge", 1024, mer}, + {"MarshalerSmall", 0, mer}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + marshalCheck(t, tc.m, tc.size) + }) + } +} + +func marshalCheck(t *testing.T, m proto.Message, size int) { + buf := proto.NewBuffer(make([]byte, 0, size)) + err := buf.Marshal(m) + if err != nil { + t.Fatal(err) + } + err = buf.Marshal(m) + if err != nil { + t.Fatal(err) + } + bufferBytes := buf.Bytes() + protoBytes, err := proto.Marshal(m) + if err != nil { + t.Fatal(err) + } + protoBytes = append(protoBytes, protoBytes...) + if !bytes.Equal(bufferBytes, protoBytes) { + t.Fatalf("proto.Buffer Marshal != proto.Marshal (%v != %v)\n", bufferBytes, protoBytes) + } +}