hcl: fix scenario where infinite loop happens

This commit is contained in:
Mitchell Hashimoto 2014-09-07 16:18:56 -07:00
parent a0a5d2873e
commit 9b5d9eb9b0
3 changed files with 15 additions and 0 deletions

View File

@ -164,6 +164,7 @@ func (x *hclLex) consumeComment(c rune) bool {
// lexId lexes an identifier // lexId lexes an identifier
func (x *hclLex) lexId(yylval *hclSymType) int { func (x *hclLex) lexId(yylval *hclSymType) int {
var b bytes.Buffer var b bytes.Buffer
first := true
for { for {
c := x.next() c := x.next()
if c == lexEOF { if c == lexEOF {
@ -172,9 +173,16 @@ func (x *hclLex) lexId(yylval *hclSymType) int {
if !unicode.IsDigit(c) && !unicode.IsLetter(c) && c != '_' && c != '-' { if !unicode.IsDigit(c) && !unicode.IsLetter(c) && c != '_' && c != '-' {
x.backup() x.backup()
if first {
x.createErr("Invalid identifier")
return lexEOF
}
break break
} }
first = false
if _, err := b.WriteRune(c); err != nil { if _, err := b.WriteRune(c); err != nil {
return lexEOF return lexEOF
} }

View File

@ -32,6 +32,10 @@ func TestLex(t *testing.T) {
RIGHTBRACKET, lexEOF, RIGHTBRACKET, lexEOF,
}, },
}, },
{
"old.hcl",
[]int{IDENTIFIER, EQUAL, LEFTBRACE, STRING, lexEOF},
},
{ {
"structure_basic.hcl", "structure_basic.hcl",
[]int{ []int{

View File

@ -0,0 +1,3 @@
default = {
"eu-west-1": "ami-b1cf19c6",
}