Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/proto: Fix encoding of zero embedded messages #672

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions internal/proto/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package proto
import (
"encoding/binary"
"math"
"reflect"

"google.golang.org/protobuf/encoding/protowire"
)
Expand Down Expand Up @@ -159,27 +160,20 @@ func MarshalToDouble(b []byte, num protowire.Number, v float64) int {
// SizeEmbedded returns the encoded size of embedded message being a protobuf
// field with given number and value.
func SizeEmbedded(num protowire.Number, v Message) int {
if v == nil {
if v == nil || reflect.ValueOf(v).IsNil() {
return 0
}
sz := v.MarshaledSize()
if sz == 0 {
return 0
}
return protowire.SizeTag(num) + protowire.SizeBytes(sz)
return protowire.SizeTag(num) + protowire.SizeBytes(v.MarshaledSize())
}

// MarshalToEmbedded encodes embedded message being a protobuf field with given
// number and value into b and returns the number of bytes written. If the
// buffer is too small, MarshalToEmbedded will panic.
func MarshalToEmbedded(b []byte, num protowire.Number, v Message) int {
if v == nil {
if v == nil || reflect.ValueOf(v).IsNil() {
return 0
}
sz := v.MarshaledSize()
if sz == 0 {
return 0
}
off := binary.PutUvarint(b, protowire.EncodeTag(num, protowire.BytesType))
off += binary.PutUvarint(b[off:], uint64(sz))
v.MarshalStable(b[off:])
Expand Down
21 changes: 15 additions & 6 deletions internal/proto/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,21 @@ func BenchmarkMarshalBytes(b *testing.B) {

func TestEmbedded(t *testing.T) {
fieldNum := randFieldNum()
require.Zero(t, proto.SizeEmbedded(fieldNum, nil))
require.Zero(t, proto.MarshalToEmbedded(nil, fieldNum, nil))
require.Zero(t, proto.SizeEmbedded(fieldNum, (*timestamp)(nil)))
require.Zero(t, proto.MarshalToEmbedded(nil, fieldNum, (*timestamp)(nil)))
require.Zero(t, proto.SizeEmbedded(fieldNum, new(timestamp)))
require.Zero(t, proto.MarshalToEmbedded(nil, fieldNum, new(timestamp)))
t.Run("nil", func(t *testing.T) {
require.Zero(t, proto.SizeEmbedded(fieldNum, nil))
require.Zero(t, proto.MarshalToEmbedded(nil, fieldNum, nil))
require.Zero(t, proto.SizeEmbedded(fieldNum, (*timestamp)(nil)))
require.Zero(t, proto.MarshalToEmbedded(nil, fieldNum, (*timestamp)(nil)))
})
t.Run("zero", func(t *testing.T) {
const fieldNum = 480010005
const expLen = 6
tz := new(timestamp)
require.EqualValues(t, expLen, proto.SizeEmbedded(fieldNum, tz))
b := make([]byte, expLen)
proto.MarshalToEmbedded(b, fieldNum, tz)
require.Equal(t, []byte{170, 241, 139, 167, 14, 0}, b) // first 5 bytes is num, last zero is size
})

v := (*timestamp)(timestamppb.Now())
sz := proto.SizeEmbedded(fieldNum, v)
Expand Down
Loading