diff --git a/encoding/prototext/decode_test.go b/encoding/prototext/decode_test.go index 3597d9b6f..2f8b59607 100644 --- a/encoding/prototext/decode_test.go +++ b/encoding/prototext/decode_test.go @@ -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{}, diff --git a/internal/encoding/text/decode_number.go b/internal/encoding/text/decode_number.go index 3dc8e9787..45c81f029 100644 --- a/internal/encoding/text/decode_number.go +++ b/internal/encoding/text/decode_number.go @@ -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 { diff --git a/internal/encoding/text/decode_test.go b/internal/encoding/text/decode_test.go index 528b1bc71..7c705ab51 100644 --- a/internal/encoding/text/decode_test.go +++ b/internal/encoding/text/decode_test.go @@ -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"}},