From 1c284ec98f4b398443cbabb0d9197f7f4cc0077c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 10 Feb 2016 10:31:11 -0800 Subject: [PATCH] Parser enforces closing RBRACE [GH-88] --- decoder_test.go | 9 +++++++-- hcl/parser/parser.go | 5 +++++ hcl/parser/parser_test.go | 6 ++++++ hcl/parser/test-fixtures/unterminated_object.hcl | 2 ++ test-fixtures/unterminated_brace.hcl | 2 ++ 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 hcl/parser/test-fixtures/unterminated_object.hcl create mode 100644 test-fixtures/unterminated_brace.hcl diff --git a/decoder_test.go b/decoder_test.go index c02bb57..e976271 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -238,6 +238,12 @@ func TestDecode_interface(t *testing.T) { nil, }, + { + "unterminated_brace.hcl", + true, + nil, + }, + { "object_list.json", false, @@ -629,7 +635,7 @@ content { func TestDecode_NestedNode(t *testing.T) { // given var value struct { - Nested struct { + Nested struct { Content ast.Node } } @@ -661,4 +667,3 @@ nested "content" { t.Errorf("expected mapping to be returned") } } - diff --git a/hcl/parser/parser.go b/hcl/parser/parser.go index c951bf3..086c087 100644 --- a/hcl/parser/parser.go +++ b/hcl/parser/parser.go @@ -246,6 +246,11 @@ func (p *Parser) objectType() (*ast.ObjectType, error) { return nil, err } + // If there is no error, we should be at a RBRACE to end the object + if p.tok.Type != token.RBRACE { + return nil, fmt.Errorf("object expected closing RBRACE got: %s", p.tok.Type) + } + o.List = l o.Rbrace = p.tok.Pos // advanced via parseObjectList return o, nil diff --git a/hcl/parser/parser_test.go b/hcl/parser/parser_test.go index af142f8..2ea95c1 100644 --- a/hcl/parser/parser_test.go +++ b/hcl/parser/parser_test.go @@ -156,6 +156,7 @@ func TestObjectType(t *testing.T) { item, err := p.objectItem() if err != nil { t.Error(err) + continue } // we know that the ObjectKey name is foo for all cases, what matters @@ -163,6 +164,7 @@ func TestObjectType(t *testing.T) { obj, ok := item.Val.(*ast.ObjectType) if !ok { t.Errorf("node should be of type LiteralType, got: %T", item.Val) + continue } // check if the total length of items are correct @@ -297,6 +299,10 @@ func TestParse(t *testing.T) { "missing_braces.hcl", true, }, + { + "unterminated_object.hcl", + true, + }, } const fixtureDir = "./test-fixtures" diff --git a/hcl/parser/test-fixtures/unterminated_object.hcl b/hcl/parser/test-fixtures/unterminated_object.hcl new file mode 100644 index 0000000..31b37c4 --- /dev/null +++ b/hcl/parser/test-fixtures/unterminated_object.hcl @@ -0,0 +1,2 @@ +foo "baz" { + bar = "baz" diff --git a/test-fixtures/unterminated_brace.hcl b/test-fixtures/unterminated_brace.hcl new file mode 100644 index 0000000..31b37c4 --- /dev/null +++ b/test-fixtures/unterminated_brace.hcl @@ -0,0 +1,2 @@ +foo "baz" { + bar = "baz"