Skip to content

Commit

Permalink
Fix confusion between slice capacity and length
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Pelisse committed Sep 9, 2019
1 parent f6b4f34 commit d0d6954
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion proto/table_marshal.go
Expand Up @@ -2936,7 +2936,7 @@ func Size(pb Message) int {
func Marshal(pb Message) ([]byte, error) {
if m, ok := pb.(newMarshaler); ok {
siz := m.XXX_Size()
b := make([]byte, 0, siz)
b := make([]byte, siz)
return m.XXX_Marshal(b, false)
}
if m, ok := pb.(Marshaler); ok {
Expand Down Expand Up @@ -2969,6 +2969,7 @@ 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 = p.buf[:siz]
p.buf, err = m.XXX_Marshal(p.buf, p.deterministic)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions protoc-gen-gogo/generator/generator.go
Expand Up @@ -2717,7 +2717,7 @@ func (g *Generator) generateCommonMethods(mc *msgCtx) {
if gogoproto.IsMarshaler(g.file.FileDescriptorProto, mc.message.DescriptorProto) ||
gogoproto.IsUnsafeMarshaler(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
if gogoproto.IsStableMarshaler(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
g.P("b = b[:cap(b)]")
g.P("b = b[:len(b)]")
g.P("n, err := m.MarshalToSizedBuffer(b)")
g.P("if err != nil {")
g.In()
Expand All @@ -2731,7 +2731,7 @@ func (g *Generator) generateCommonMethods(mc *msgCtx) {
g.P("return xxx_messageInfo_", mc.goName, ".Marshal(b, m, deterministic)")
g.P("} else {")
g.In()
g.P("b = b[:cap(b)]")
g.P("b = b[:len(b)]")
g.P("n, err := m.MarshalToSizedBuffer(b)")
g.P("if err != nil {")
g.In()
Expand Down
2 changes: 1 addition & 1 deletion test/protobuffer/protobuffer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d0d6954

Please sign in to comment.