From 5ba0c29655be05b4aa816fbb0bbf7d36f20ea4d8 Mon Sep 17 00:00:00 2001 From: Damien Neil Date: Sat, 21 Dec 2019 09:45:00 -0800 Subject: [PATCH] internal/encoding/json: fix crash in parsing Fuzzer-detected crash when parsing: {"" Change-Id: I019c667f48e6a1237858b5abf7d34f43593fb3b6 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/212357 Reviewed-by: Herbie Ong --- internal/encoding/json/decode.go | 3 +++ internal/encoding/json/decode_test.go | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/internal/encoding/json/decode.go b/internal/encoding/json/decode.go index ae2ee9ab..3e58d069 100644 --- a/internal/encoding/json/decode.go +++ b/internal/encoding/json/decode.go @@ -102,6 +102,9 @@ func (d *Decoder) Read() (Value, error) { } d.in = d.in[n:] d.consume(0) + if len(d.in) == 0 { + return Value{}, d.newSyntaxError(`unexpected EOF, missing ":" after object name`) + } if c := d.in[0]; c != ':' { return Value{}, d.newSyntaxError(`unexpected character %v, missing ":" after object name`, string(c)) } diff --git a/internal/encoding/json/decode_test.go b/internal/encoding/json/decode_test.go index c29d5087..c896a2ea 100644 --- a/internal/encoding/json/decode_test.go +++ b/internal/encoding/json/decode_test.go @@ -872,6 +872,13 @@ func TestDecoder(t *testing.T) { {E: `unexpected EOF`}, }, }, + { + input: `{""`, + want: []R{ + {T: json.StartObject}, + {E: `syntax error (line 1:4): unexpected EOF`}, + }, + }, { input: `{"34":"89",}`, want: []R{