Skip to content

Commit

Permalink
internal/encoding/text: fix parsing of incomplete numbers
Browse files Browse the repository at this point in the history
Fix a panic when parsing the incomplete negative number "- ".

Fixes golang/protobuf#1530

Change-Id: Iba5e8ee68d5f7255c28f1a74f31beee36c9ed847
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/475995
Reviewed-by: Lasse Folger <[email protected]>
Run-TryBot: Damien Neil <[email protected]>
  • Loading branch information
neild authored and lfolger committed Mar 14, 2023
1 parent fe5bc54 commit edaf511
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions encoding/prototext/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ s_string: "谷歌"
inputMessage: &pb3.Scalars{},
inputText: "s_sfixed64: bad",
wantErr: "invalid value for sfixed64",
}, {
desc: "incomplete number value",
inputMessage: &pb3.Scalars{},
inputText: `s_int32: - `,
wantErr: "(line 1:10): invalid scalar value: -",
}, {
desc: "conformance: FloatFieldMaxValue",
inputMessage: &pb2.Scalars{},
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/text/decode_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ func parseNumber(input []byte) number {
neg = true
s = s[1:]
size++
if len(s) == 0 {
return number{}
}
// Consume any whitespace or comments between the
// negative sign and the rest of the number
lenBefore := len(s)
s = consume(s, 0)
sep = lenBefore - len(s)
size += sep
if len(s) == 0 {
return number{}
}
}

switch {
Expand Down
8 changes: 8 additions & 0 deletions internal/encoding/text/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,14 @@ func TestDecoder(t *testing.T) {
in: "- \t 123.321e6",
want: []R{{E: "invalid field number: -123.321e6"}},
},
{
in: "-",
want: []R{{E: "invalid field name: -"}},
},
{
in: "- ",
want: []R{{E: "invalid field name: -"}},
},
{
in: "- # negative\n 123",
want: []R{{E: "invalid field number: -123"}},
Expand Down

0 comments on commit edaf511

Please sign in to comment.