Skip to content

Commit

Permalink
prototext: Fix parsing of unknown repeated message fields
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
orn688 authored and neild committed Sep 27, 2023
1 parent 8088bf8 commit 6352dec
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion encoding/prototext/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
5 changes: 5 additions & 0 deletions encoding/prototext/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 6352dec

Please sign in to comment.