From e6bb065d4ec5cd70d056212d9616f0c044074ffb Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Sun, 7 Feb 2016 00:10:41 +0100 Subject: [PATCH] Parsing of incomplete maps is now safe. --- parse/parse.go | 17 ++++++++--------- parse/parse_test.go | 2 ++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/parse/parse.go b/parse/parse.go index ed77af5a..be6119fd 100644 --- a/parse/parse.go +++ b/parse/parse.go @@ -672,7 +672,7 @@ func (pn *Primary) lbracket(ps *parser) { ps.next() r := ps.peek() switch { - case isSpace(r), r == ']': + case isSpace(r), r == ']', r == EOF: // '&' { Space } ']': '&' is a sep addSep(pn, ps) parseSpaces(pn, ps) @@ -789,19 +789,18 @@ type MapPair struct { func (mpn *MapPair) parse(ps *parser) { parseSep(mpn, ps, '&') - parseSpaces(mpn, ps) - if !startsCompound(ps.peek()) { - ps.error(shouldBeCompound) - return - } - mpn.setKey(parseCompound(ps)) parseSpaces(mpn, ps) - if !startsCompound(ps.peek()) { + mpn.setKey(parseCompound(ps)) + if len(mpn.Key.Indexings) == 0 { ps.error(shouldBeCompound) - return } + + parseSpaces(mpn, ps) mpn.setValue(parseCompound(ps)) + if len(mpn.Value.Indexings) == 0 { + ps.error(shouldBeCompound) + } } // Sep is the catch-all node type for leaf nodes that lack internal structures diff --git a/parse/parse_test.go b/parse/parse_test.go index 36bd3bc9..febde3af 100644 --- a/parse/parse_test.go +++ b/parse/parse_test.go @@ -329,6 +329,8 @@ var badCases = []struct { {")", 0}, {"]", 0}, {"}", 0}, // Unclosed parens. {"a (", 3}, {"a [", 3}, {"a {", 3}, + // Ampersand + {"a &", 3}, {"a [&", 4}, } func TestParseError(t *testing.T) {