]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.17] text/template: initialize template before locking it
authorIan Lance Taylor <iant@golang.org>
Fri, 17 Sep 2021 19:27:23 +0000 (12:27 -0700)
committerIan Lance Taylor <iant@golang.org>
Thu, 23 Sep 2021 21:18:37 +0000 (21:18 +0000)
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

src/text/template/multi_test.go
src/text/template/template.go

index b543ab5c47c6d3f06c57b20ce5993f57f63df32f..6b81ffe7acbb5806154ad0430c26638c4cd23a2d 100644 (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"])
+}
index fd74d45e9b1c6df9a0bf45206eba1d040dd0d3b9..776be9cd075d37dc9863c6dccdf551298d9a0a77 100644 (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)