From 9b5d9eb9b09475889ae49a4a613c60280875b3d1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 7 Sep 2014 16:18:56 -0700 Subject: [PATCH] hcl: fix scenario where infinite loop happens --- hcl/lex.go | 8 ++++++++ hcl/lex_test.go | 4 ++++ hcl/test-fixtures/old.hcl | 3 +++ 3 files changed, 15 insertions(+) create mode 100644 hcl/test-fixtures/old.hcl diff --git a/hcl/lex.go b/hcl/lex.go index 8d710e6..46d5bdc 100644 --- a/hcl/lex.go +++ b/hcl/lex.go @@ -164,6 +164,7 @@ func (x *hclLex) consumeComment(c rune) bool { // lexId lexes an identifier func (x *hclLex) lexId(yylval *hclSymType) int { var b bytes.Buffer + first := true for { c := x.next() if c == lexEOF { @@ -172,9 +173,16 @@ func (x *hclLex) lexId(yylval *hclSymType) int { if !unicode.IsDigit(c) && !unicode.IsLetter(c) && c != '_' && c != '-' { x.backup() + + if first { + x.createErr("Invalid identifier") + return lexEOF + } + break } + first = false if _, err := b.WriteRune(c); err != nil { return lexEOF } diff --git a/hcl/lex_test.go b/hcl/lex_test.go index aa8358a..cd87573 100644 --- a/hcl/lex_test.go +++ b/hcl/lex_test.go @@ -32,6 +32,10 @@ func TestLex(t *testing.T) { RIGHTBRACKET, lexEOF, }, }, + { + "old.hcl", + []int{IDENTIFIER, EQUAL, LEFTBRACE, STRING, lexEOF}, + }, { "structure_basic.hcl", []int{ diff --git a/hcl/test-fixtures/old.hcl b/hcl/test-fixtures/old.hcl new file mode 100644 index 0000000..e9f77ca --- /dev/null +++ b/hcl/test-fixtures/old.hcl @@ -0,0 +1,3 @@ +default = { + "eu-west-1": "ami-b1cf19c6", +}