1
0
mirror of https://github.com/golang/go synced 2024-09-23 15:20:13 -06:00

text/template: initialize template before locking it

For #39807
Fixes #48436

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>
This commit is contained in:
Ian Lance Taylor 2021-09-17 12:27:23 -07:00
parent 6e81f78c0f
commit ba1c52d7d7
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)