internal/encoding/text: fix parsing of incomplete numbers

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 <lassefolger@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
This commit is contained in:
Damien Neil 2023-03-13 11:21:46 -07:00 committed by Lasse Folger
parent fe5bc54041
commit edaf511a7a
3 changed files with 16 additions and 3 deletions

View File

@ -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{},

View File

@ -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 {

View File

@ -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"}},