From c30776bb3b86eeeb72685d659c4ae18ed5b4838a Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Wed, 5 May 2021 15:13:31 -0700 Subject: [PATCH] encoding/prototext: fix skipping of unknown fields Inside decoder.skipValue we should not be calling skipValue again since we had already read the value earlier. The only possible composite type in the context of a list is another message, which is already handled in the case above. Change-Id: If40da2d369e0a64a64ba9b961377331231158fe2 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/317430 Trust: Joe Tsai Trust: Herbie Ong Reviewed-by: Herbie Ong --- encoding/prototext/decode.go | 3 --- encoding/prototext/decode_test.go | 11 +++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/encoding/prototext/decode.go b/encoding/prototext/decode.go index 8fb1d9e0..179d6e8f 100644 --- a/encoding/prototext/decode.go +++ b/encoding/prototext/decode.go @@ -744,9 +744,6 @@ func (d decoder) skipValue() error { // Skip items. This will not validate whether skipped values are // of the same type or not, same behavior as C++ // TextFormat::Parser::AllowUnknownField(true) version 3.8.0. - if err := d.skipValue(); err != nil { - return err - } } } } diff --git a/encoding/prototext/decode_test.go b/encoding/prototext/decode_test.go index 441dcb24..2e92dd20 100644 --- a/encoding/prototext/decode_test.go +++ b/encoding/prototext/decode_test.go @@ -225,6 +225,17 @@ s_string: "谷歌" inputMessage: &pb2.Scalars{}, inputText: `13:"hello"`, wantErr: "cannot specify field by number", + }, { + desc: "unknown list field", + umo: prototext.UnmarshalOptions{DiscardUnknown: true}, + inputMessage: &pb2.Scalars{}, + inputText: `unknown_field: { strings: [ "" ] }`, + }, { + desc: "unknown list of list field", + umo: prototext.UnmarshalOptions{DiscardUnknown: true}, + inputMessage: &pb2.Scalars{}, + inputText: `unknown_field: { strings: [ [ ] ] }`, + wantErr: `(line 1:29): invalid scalar value: [`, }, { desc: "proto3 message cannot parse field number", umo: prototext.UnmarshalOptions{DiscardUnknown: true},