Parsing of incomplete maps is now safe.

This commit is contained in:
Qi Xiao 2016-02-07 00:10:41 +01:00
parent 74ae20cb8e
commit e6bb065d4e
2 changed files with 10 additions and 9 deletions

View File

@ -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

View File

@ -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) {