From 6fa08c0fdbc8435d0a7b0c2576ba2183adfac8f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Wed, 29 Aug 2018 07:06:31 -0600 Subject: [PATCH] text/template: fix newline counting in raw strings MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit lexRawQuote already uses the next method, which keeps track of newlines on a character by character basis. Adding up newlines in emit again results in the newlines being counted twice, which can mean bad position information in error messages. Fix that, and add a test. Fixes #27319. Change-Id: Id803be065c541412dc808d388bc6d8a86a0de41e Reviewed-on: https://go-review.googlesource.com/131996 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/text/template/parse/lex.go | 2 +- src/text/template/parse/parse_test.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/text/template/parse/lex.go b/src/text/template/parse/lex.go index fc259f351e..c0843af6ed 100644 --- a/src/text/template/parse/lex.go +++ b/src/text/template/parse/lex.go @@ -155,7 +155,7 @@ func (l *lexer) emit(t itemType) { l.items <- item{t, l.start, l.input[l.start:l.pos], l.line} // Some items contain text internally. If so, count their newlines. switch t { - case itemText, itemRawString, itemLeftDelim, itemRightDelim: + case itemText, itemLeftDelim, itemRightDelim: l.line += strings.Count(l.input[l.start:l.pos], "\n") } l.start = l.pos diff --git a/src/text/template/parse/parse_test.go b/src/text/template/parse/parse_test.go index c1f80c1326..d03987581c 100644 --- a/src/text/template/parse/parse_test.go +++ b/src/text/template/parse/parse_test.go @@ -447,6 +447,9 @@ var errorTests = []parseTest{ {"emptypipeline", `{{ ( ) }}`, hasError, `missing value for parenthesized pipeline`}, + {"multilinerawstring", + "{{ $v := `\n` }} {{", + hasError, `multilinerawstring:2: unexpected unclosed action`}, } func TestErrors(t *testing.T) { -- 2.50.0