From 8bad6ac32efb6946eae2c3bf21f32f3dea93c3cf Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 4 Apr 2018 17:07:08 +0200 Subject: [PATCH 1/2] scanner: Add tests demonstrating issue with empty heredoc anchors. --- hcl/printer/printer_test.go | 1 + hcl/scanner/scanner_test.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/hcl/printer/printer_test.go b/hcl/printer/printer_test.go index 15a4754..0f41954 100644 --- a/hcl/printer/printer_test.go +++ b/hcl/printer/printer_test.go @@ -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 { diff --git a/hcl/scanner/scanner_test.go b/hcl/scanner/scanner_test.go index 0890153..58d68f5 100644 --- a/hcl/scanner/scanner_test.go +++ b/hcl/scanner/scanner_test.go @@ -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) { From a68b5db4c3cfe504ada21d586298d1b554141455 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 4 Apr 2018 17:09:20 +0200 Subject: [PATCH 2/2] scanner: Fix detection of zero-length heredoc anchor. --- hcl/scanner/scanner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hcl/scanner/scanner.go b/hcl/scanner/scanner.go index 9cd84e2..624a18f 100644 --- a/hcl/scanner/scanner.go +++ b/hcl/scanner/scanner.go @@ -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 }