diff options
author | Ian Lance Taylor <iant@golang.org> | 2021-09-17 12:27:23 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2021-09-23 21:18:37 +0000 |
commit | c5c1d069da73a5e74bd2139ef1c7c14659915acd (patch) | |
tree | c917b16edf734b5814ee9f51c6c13a4367b9369c | |
parent | abc4f092acf9bb65e56fcbd6ee7b2c76cee57ad7 (diff) | |
download | go-git-c5c1d069da73a5e74bd2139ef1c7c14659915acd.tar.gz |
[release-branch.go1.17] text/template: initialize template before locking it
For #39807
For #48436
Fixes #48444
Change-Id: I75f82fd8738dd2f11f0c69b1230e1be1abc36024
Reviewed-on: https://go-review.googlesource.com/c/go/+/350730
Trust: Ian Lance Taylor <iant@golang.org>
Trust: Daniel Martà <mvdan@mvdan.cc>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Daniel Martà <mvdan@mvdan.cc>
(cherry picked from commit ba1c52d7d77724de4407572bd02421c36df3d78a)
Reviewed-on: https://go-review.googlesource.com/c/go/+/351115
-rw-r--r-- | src/text/template/multi_test.go | 10 | ||||
-rw-r--r-- | src/text/template/template.go | 2 |
2 files changed, 11 insertions, 1 deletions
diff --git a/src/text/template/multi_test.go b/src/text/template/multi_test.go index b543ab5c47..6b81ffe7ac 100644 --- a/src/text/template/multi_test.go +++ b/src/text/template/multi_test.go @@ -452,3 +452,13 @@ func TestIssue19294(t *testing.T) { } } } + +// Issue 48436 +func TestAddToZeroTemplate(t *testing.T) { + tree, err := parse.Parse("c", cloneText3, "", "", nil, builtins()) + if err != nil { + t.Fatal(err) + } + var tmpl Template + tmpl.AddParseTree("x", tree["c"]) +} diff --git a/src/text/template/template.go b/src/text/template/template.go index fd74d45e9b..776be9cd07 100644 --- a/src/text/template/template.go +++ b/src/text/template/template.go @@ -127,9 +127,9 @@ func (t *Template) copy(c *common) *Template { // its definition. If it has been defined and already has that name, the existing // definition is replaced; otherwise a new template is created, defined, and returned. func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) { + t.init() t.muTmpl.Lock() defer t.muTmpl.Unlock() - t.init() nt := t if name != t.name { nt = t.New(name) |