Skip to content

Commit

Permalink
internal/impl: better fast-path init checks for extensions
Browse files Browse the repository at this point in the history
Unknown extensions are initialized.

Valid extensions with no isInit func are initialized.

Change-Id: I2975c7ef85d2b777eca467d3b1861d20de8e24fc
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/216960
Reviewed-by: Joe Tsai <joetsai@google.com>
  • Loading branch information
neild committed Jan 30, 2020
1 parent 6f29779 commit 1887ff7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions internal/impl/decode.go
Expand Up @@ -140,6 +140,9 @@ func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag wire.Numbe
}
var o unmarshalOutput
o, err = mi.unmarshalExtension(b, num, wtyp, *exts, opts)
if err != nil {
break
}
n = o.n
if !o.initialized {
initialized = false
Expand Down Expand Up @@ -213,6 +216,9 @@ func (mi *MessageInfo) unmarshalExtension(b []byte, num wire.Number, wtyp wire.T
if err != nil {
return out, err
}
if xi.funcs.isInit == nil {
out.initialized = true
}
x.Set(xt, v)
exts[int32(num)] = x
return out, nil
Expand Down
11 changes: 9 additions & 2 deletions proto/testmessages_test.go
Expand Up @@ -32,7 +32,8 @@ type testProto struct {

var testValidMessages = []testProto{
{
desc: "basic scalar types",
desc: "basic scalar types",
checkFastInit: true,
decodeTo: []proto.Message{&testpb.TestAllTypes{
OptionalInt32: proto.Int32(1001),
OptionalInt64: proto.Int64(1002),
Expand Down Expand Up @@ -1106,7 +1107,8 @@ var testValidMessages = []testProto{
// considers equivalent to those of the v1 decoder. Figure out if
// that's a problem or not.
{
desc: "unknown fields",
desc: "unknown fields",
checkFastInit: true,
decodeTo: []proto.Message{build(
&testpb.TestAllTypes{},
unknown(pack.Message{
Expand All @@ -1117,6 +1119,11 @@ var testValidMessages = []testProto{
unknown(pack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(1),
}.Marshal()),
), build(
&testpb.TestAllExtensions{},
unknown(pack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(1),
}.Marshal()),
)},
wire: pack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(1),
Expand Down

0 comments on commit 1887ff7

Please sign in to comment.