[release-branch.go1.16] text/template: initialize template before locking it

For #39807
For #48436
Fixes #48443

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 ba1c52d7d7)
Reviewed-on: https://go-review.googlesource.com/c/go/+/351116
This commit is contained in:
Ian Lance Taylor 2021-09-17 12:27:23 -07:00
parent 552410fec2
commit 7d57324030
2 changed files with 11 additions and 1 deletions

View File

@ -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"])
}

View File

@ -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)