prototext: Fix parsing of unknown repeated message fields

If `DiscardUnknown` was enabled, previously this syntax was rejected
with an error "unexpected token: ]":

  unknown_field: [
    {}
  ]

This is because the parser short-circuited after parsing the {}, and
didn't handle the closing square brace. Now it no longer short-circuits.

Change-Id: I6230b56766632752a5cc8822b17ed8262873d6cc
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/530616
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
This commit is contained in:
Oliver Newman 2023-09-26 14:56:57 -04:00 committed by Damien Neil
parent 8088bf85b8
commit 6352deccdb
2 changed files with 8 additions and 1 deletions

View File

@ -739,7 +739,9 @@ func (d decoder) skipValue() error {
case text.ListClose:
return nil
case text.MessageOpen:
return d.skipMessageValue()
if err := d.skipMessageValue(); err != nil {
return err
}
default:
// Skip items. This will not validate whether skipped values are
// of the same type or not, same behavior as C++

View File

@ -237,6 +237,11 @@ s_string: "谷歌"
inputMessage: &pb2.Scalars{},
inputText: `unknown_field: { strings: [ [ ] ] }`,
wantErr: `(line 1:29): invalid scalar value: [`,
}, {
desc: "unknown list of message field",
umo: prototext.UnmarshalOptions{DiscardUnknown: true},
inputMessage: &pb2.Scalars{},
inputText: `unknown_field: [ { a: "b" }, { c: "d" } ]`,
}, {
desc: "proto3 message cannot parse field number",
umo: prototext.UnmarshalOptions{DiscardUnknown: true},