From eca76d650c654a404040cfd383d2353508004f8b Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 17 May 2017 07:59:41 -0700 Subject: [PATCH] json: Detect and report extraneous data at EOF --- zcl/json/parser.go | 8 ++++++++ zcl/json/parser_test.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/zcl/json/parser.go b/zcl/json/parser.go index ef35985..1c3924f 100644 --- a/zcl/json/parser.go +++ b/zcl/json/parser.go @@ -19,6 +19,14 @@ func parseFileContent(buf []byte, filename string) (node, zcl.Diagnostics) { }) p := newPeeker(tokens) node, diags := parseValue(p) + if len(diags) == 0 && p.Peek().Type != tokenEOF { + diags = diags.Append(&zcl.Diagnostic{ + Severity: zcl.DiagError, + Summary: "Extraneous data after value", + Detail: "Extra characters appear after the JSON value.", + Subject: p.Peek().Range.Ptr(), + }) + } if diags.HasErrors() { // Don't return a node if there were errors during parsing. return nil, diags diff --git a/zcl/json/parser_test.go b/zcl/json/parser_test.go index a5ba789..8bb7a93 100644 --- a/zcl/json/parser_test.go +++ b/zcl/json/parser_test.go @@ -188,6 +188,11 @@ func TestParse(t *testing.T) { nil, 1, }, + { + `1 2`, + nil, + 1, + }, // Objects {