From ad6a17cba6381eebff346ab26689e9d02a75f2e0 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Tue, 4 May 2021 11:57:55 -0400 Subject: Fix parseQuotedStringLiteral zero end range If a block label had an invalid quoted string literal, the parser would previously create label ranges with uninitialized `End` values. For Terraform, this would eventually percolate into invalid diagnostic output due to the assumption that the end of a range is always greater than or equal to the start. This commit addresses this by using the terminating token's position as the end of the range, even if it's not a closing quote. --- hclsyntax/parser_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'hclsyntax/parser_test.go') diff --git a/hclsyntax/parser_test.go b/hclsyntax/parser_test.go index 45b5645..8b083da 100644 --- a/hclsyntax/parser_test.go +++ b/hclsyntax/parser_test.go @@ -2484,6 +2484,64 @@ block "valid" {} }, }, }, + { + `block "unterminated_string "name" {}`, + 2, // "Invalid string literal" and "Invalid block definition" + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{ + &Block{ + Type: "block", + Labels: []string{"unterminated_string ", "name", " {}"}, + Body: &Body{ + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + }, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + LabelRanges: []hcl.Range{ + { + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 29, Byte: 28}, + }, + { + Start: hcl.Pos{Line: 1, Column: 29, Byte: 28}, + End: hcl.Pos{Line: 1, Column: 33, Byte: 32}, + }, + { + Start: hcl.Pos{Line: 1, Column: 33, Byte: 32}, + End: hcl.Pos{Line: 1, Column: 37, Byte: 36}, + }, + }, + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 37, Byte: 36}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 37, Byte: 36}, + End: hcl.Pos{Line: 1, Column: 37, Byte: 36}, + }, + }, + }, } for _, test := range tests { -- cgit v1.2.3