Fix conf parser hang on dangling quote

Signed-off-by: Waldemar Quevedo <wally@synadia.com>
This commit is contained in:
Waldemar Quevedo
2018-10-23 15:14:22 -07:00
parent 77548d493c
commit e8928b7eed
2 changed files with 96 additions and 0 deletions

View File

@@ -346,6 +346,12 @@ func lexDubQuotedKey(lx *lexer) stateFn {
lx.emit(itemKey) lx.emit(itemKey)
lx.next() lx.next()
return lexSkip(lx, lexKeyEnd) return lexSkip(lx, lexKeyEnd)
} else if r == eof {
if lx.pos > lx.start {
return lx.errorf("Unexpected EOF.")
}
lx.emit(itemEOF)
return nil
} }
lx.next() lx.next()
return lexDubQuotedKey return lexDubQuotedKey
@@ -358,6 +364,12 @@ func lexQuotedKey(lx *lexer) stateFn {
lx.emit(itemKey) lx.emit(itemKey)
lx.next() lx.next()
return lexSkip(lx, lexKeyEnd) return lexSkip(lx, lexKeyEnd)
} else if r == eof {
if lx.pos > lx.start {
return lx.errorf("Unexpected EOF.")
}
lx.emit(itemEOF)
return nil
} }
lx.next() lx.next()
return lexQuotedKey return lexQuotedKey
@@ -788,6 +800,12 @@ func lexQuotedString(lx *lexer) stateFn {
lx.next() lx.next()
lx.ignore() lx.ignore()
return lx.pop() return lx.pop()
case r == eof:
if lx.pos > lx.start {
return lx.errorf("Unexpected EOF.")
}
lx.emit(itemEOF)
return nil
} }
return lexQuotedString return lexQuotedString
} }
@@ -807,6 +825,12 @@ func lexDubQuotedString(lx *lexer) stateFn {
lx.next() lx.next()
lx.ignore() lx.ignore()
return lx.pop() return lx.pop()
case r == eof:
if lx.pos > lx.start {
return lx.errorf("Unexpected EOF.")
}
lx.emit(itemEOF)
return nil
} }
return lexDubQuotedString return lexDubQuotedString
} }

View File

@@ -958,6 +958,78 @@ func TestNonQuotedStrings(t *testing.T) {
expect(t, lx, expectedItems) expect(t, lx, expectedItems)
} }
var danglingquote = `
listen: "localhost:4242
http: localhost:8222
`
func TestDanglingQuotedString(t *testing.T) {
expectedItems := []item{
{itemKey, "listen", 2, 1},
{itemError, "Unexpected EOF.", 5, 1},
}
lx := lex(danglingquote)
expect(t, lx, expectedItems)
}
var keydanglingquote = `
foo = "
listen: "
http: localhost:8222
"
`
func TestKeyDanglingQuotedString(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2, 1},
{itemString, "\nlisten: ", 3, 8},
{itemKey, "http", 5, 1},
{itemString, "localhost:8222", 5, 7},
{itemError, "Unexpected EOF.", 8, 1},
}
lx := lex(keydanglingquote)
expect(t, lx, expectedItems)
}
var danglingsquote = `
listen: 'localhost:4242
http: localhost:8222
`
func TestDanglingSingleQuotedString(t *testing.T) {
expectedItems := []item{
{itemKey, "listen", 2, 1},
{itemError, "Unexpected EOF.", 5, 1},
}
lx := lex(danglingsquote)
expect(t, lx, expectedItems)
}
var keydanglingsquote = `
foo = '
listen: '
http: localhost:8222
'
`
func TestKeyDanglingSingleQuotedString(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2, 1},
{itemString, "\nlisten: ", 3, 8},
{itemKey, "http", 5, 1},
{itemString, "localhost:8222", 5, 7},
{itemError, "Unexpected EOF.", 8, 1},
}
lx := lex(keydanglingsquote)
expect(t, lx, expectedItems)
}
func TestMapQuotedKeys(t *testing.T) { func TestMapQuotedKeys(t *testing.T) {
expectedItems := []item{ expectedItems := []item{
{itemKey, "foo", 1, 0}, {itemKey, "foo", 1, 0},