Skip to content

Commit 09ab773

Browse files
authored
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
1 parent 8142193 commit 09ab773

File tree

11 files changed

+806
-1
lines changed

11 files changed

+806
-1
lines changed

Makefile

100755100644
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ regenerate:
137137
make -C test/issue503 regenerate
138138
make -C test/issue530 regenerate
139139
make -C test/issue617 regenerate
140+
make -C test/issue620 regenerate
141+
make -C test/protobuffer regenerate
140142
make -C test/issue630 regenerate
141143

142144
make gofmt

proto/encode.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ type Marshaler interface {
189189
// prefixed by a varint-encoded length.
190190
func (p *Buffer) EncodeMessage(pb Message) error {
191191
siz := Size(pb)
192+
sizVar := SizeVarint(uint64(siz))
193+
p.grow(siz + sizVar)
192194
p.EncodeVarint(uint64(siz))
193195
return p.Marshal(pb)
194196
}

proto/table_marshal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2969,7 +2969,9 @@ func (p *Buffer) Marshal(pb Message) error {
29692969
if m, ok := pb.(newMarshaler); ok {
29702970
siz := m.XXX_Size()
29712971
p.grow(siz) // make sure buf has enough capacity
2972-
p.buf, err = m.XXX_Marshal(p.buf, p.deterministic)
2972+
pp := p.buf[len(p.buf) : len(p.buf) : len(p.buf)+siz]
2973+
pp, err = m.XXX_Marshal(pp, p.deterministic)
2974+
p.buf = append(p.buf, pp...)
29732975
return err
29742976
}
29752977
if m, ok := pb.(Marshaler); ok {

test/issue620/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Protocol Buffers for Go with Gadgets
2+
#
3+
# Copyright (c) 2019, The GoGo Authors. All rights reserved.
4+
# http://github.com/gogo/protobuf
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are
8+
# met:
9+
#
10+
# * Redistributions of source code must retain the above copyright
11+
# notice, this list of conditions and the following disclaimer.
12+
# * Redistributions in binary form must reproduce the above
13+
# copyright notice, this list of conditions and the following disclaimer
14+
# in the documentation and/or other materials provided with the
15+
# distribution.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
regenerate:
30+
go install github.com/gogo/protobuf/protoc-gen-gogo
31+
protoc --proto_path=../../../../../:../../protobuf/:. --gogo_out=. issue620.proto

test/issue620/issue620.pb.go

Lines changed: 229 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/issue620/issue620.proto

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Protocol Buffers for Go with Gadgets
2+
//
3+
// Copyright (c) 2019, The GoGo Authors. All rights reserved.
4+
// http://github.com/gogo/protobuf
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are
8+
// met:
9+
//
10+
// * Redistributions of source code must retain the above copyright
11+
// notice, this list of conditions and the following disclaimer.
12+
// * Redistributions in binary form must reproduce the above
13+
// copyright notice, this list of conditions and the following disclaimer
14+
// in the documentation and/or other materials provided with the
15+
// distribution.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
syntax = "proto2";
30+
31+
package issue620;
32+
33+
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
34+
35+
message Bar {
36+
optional string Field1 = 1;
37+
}
38+
39+
message BarFast {
40+
option (gogoproto.marshaler) = true;
41+
option (gogoproto.sizer) = true;
42+
option (gogoproto.equal) = true;
43+
optional string Field1 = 1;
44+
}

0 commit comments

Comments
 (0)