Skip to content

Commit

Permalink
text/template: initialize template before locking it
Browse files Browse the repository at this point in the history
For #39807
Fixes #48436

Change-Id: I75f82fd8738dd2f11f0c69b1230e1be1abc36024
Reviewed-on: https://go-review.googlesource.com/c/go/+/350730
Trust: Ian Lance Taylor <[email protected]>
Trust: Daniel Martí <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
  • Loading branch information
ianlancetaylor committed Sep 20, 2021
1 parent 6e81f78 commit ba1c52d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/text/template/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
2 changes: 1 addition & 1 deletion src/text/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ba1c52d

Please sign in to comment.