From f1ce59d988f02deb47c3fd33cbc40542726ea76c Mon Sep 17 00:00:00 2001 From: Michael Fraenkel Date: Sat, 28 Oct 2017 20:50:57 -0400 Subject: encoding/json: Include the offset of a SyntaxError When a SyntaxError occurs, report the current offset within the stream. The code already accounted for the offset within the current buffer being scanned. By including how much data was already scanned, the current offset can be computed. Fixes #22478 Change-Id: I91ecd4cad0b85a5c1556bc597f3ee914e769af01 Reviewed-on: https://go-review.googlesource.com/74251 Reviewed-by: Joe Tsai Run-TryBot: Joe Tsai TryBot-Result: Gobot Gobot --- src/encoding/json/stream_test.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/encoding/json/stream_test.go') diff --git a/src/encoding/json/stream_test.go b/src/encoding/json/stream_test.go index d0b3ffbce9..83c01d170c 100644 --- a/src/encoding/json/stream_test.go +++ b/src/encoding/json/stream_test.go @@ -342,11 +342,18 @@ var tokenStreamCases []tokenStreamCase = []tokenStreamCase{ {json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{ Delim('['), decodeThis{map[string]interface{}{"a": float64(1)}}, - decodeThis{&SyntaxError{"expected comma after array element", 0}}, + decodeThis{&SyntaxError{"expected comma after array element", 11}}, }}, - {json: `{ "a" 1 }`, expTokens: []interface{}{ - Delim('{'), "a", - decodeThis{&SyntaxError{"expected colon after object key", 0}}, + {json: `{ "` + strings.Repeat("a", 513) + `" 1 }`, expTokens: []interface{}{ + Delim('{'), strings.Repeat("a", 513), + decodeThis{&SyntaxError{"expected colon after object key", 518}}, + }}, + {json: `{ "\a" }`, expTokens: []interface{}{ + Delim('{'), + &SyntaxError{"invalid character 'a' in string escape code", 3}, + }}, + {json: ` \a`, expTokens: []interface{}{ + &SyntaxError{"invalid character '\\\\' looking for beginning of value", 1}, }}, } @@ -367,15 +374,15 @@ func TestDecodeInStream(t *testing.T) { tk, err = dec.Token() } if experr, ok := etk.(error); ok { - if err == nil || err.Error() != experr.Error() { - t.Errorf("case %v: Expected error %v in %q, but was %v", ci, experr, tcase.json, err) + if err == nil || !reflect.DeepEqual(err, experr) { + t.Errorf("case %v: Expected error %#v in %q, but was %#v", ci, experr, tcase.json, err) } break } else if err == io.EOF { t.Errorf("case %v: Unexpected EOF in %q", ci, tcase.json) break } else if err != nil { - t.Errorf("case %v: Unexpected error '%v' in %q", ci, err, tcase.json) + t.Errorf("case %v: Unexpected error '%#v' in %q", ci, err, tcase.json) break } if !reflect.DeepEqual(tk, etk) { -- cgit v1.3-5-g9baa