Skip to content

Commit

Permalink
Merge pull request #246 from octo/zero-length-heredoc-anchor
Browse files Browse the repository at this point in the history
scanner: Fix detection of zero-length heredoc anchor.
  • Loading branch information
mitchellh authored Apr 4, 2018
2 parents 061bf37 + a68b5db commit 653ccfb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions hcl/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func TestFormatValidOutput(t *testing.T) {
"#\ue123t",
"Y=<<4\n4/\n\n\n/4/@=4/\n\n\n/4000000004\r\r\n00004\n",
"x=<<_\n_\r\r\n_\n",
"X=<<-\n\r\r\n",
}

for _, c := range cases {
Expand Down
2 changes: 1 addition & 1 deletion hcl/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (s *Scanner) scanHeredoc() {

// Read the identifier
identBytes := s.src[offs : s.srcPos.Offset-s.lastCharLen]
if len(identBytes) == 0 {
if len(identBytes) == 0 || (len(identBytes) == 1 && identBytes[0] == '-') {
s.err("zero-length heredoc anchor")
return
}
Expand Down
3 changes: 3 additions & 0 deletions hcl/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ func TestError(t *testing.T) {
testError(t, `"${abc`+"\n", "2:1", "literal not terminated", token.STRING)
testError(t, `/*/`, "1:4", "comment not terminated", token.COMMENT)
testError(t, `/foo`, "1:1", "expected '/' for comment", token.COMMENT)

testError(t, "<<\nfoo\n\n", "1:3", "zero-length heredoc anchor", token.HEREDOC)
testError(t, "<<-\nfoo\n\n", "1:4", "zero-length heredoc anchor", token.HEREDOC)
}

func testError(t *testing.T, src, pos, msg string, tok token.Type) {
Expand Down

0 comments on commit 653ccfb

Please sign in to comment.