aboutsummaryrefslogtreecommitdiff
path: root/src/html/template/template.go
diff options
context:
space:
mode:
authorSamuel Tan <samueltan@google.com>2017-09-19 11:54:47 -0700
committerRob Pike <r@golang.org>2017-09-20 01:52:02 +0000
commitcd0a5f08293e1bf1fac41ae6438d495318cd52fb (patch)
treefd8dbd0a3a0a872ffe174a2789aa61055e7d2459 /src/html/template/template.go
parent3844e707f699198ba44eaf911cbaf673aed8f286 (diff)
downloadgo-cd0a5f08293e1bf1fac41ae6438d495318cd52fb.tar.xz
html/template: prevent aliasing of parse Trees via AddParseTree
Check all associated templates in the set for an existing reference to the given Tree in AddParseTree before assigning that reference to a new or existing template. This prevents multiple html/template Templates from referencing and modifying the same underlying Tree. While there, fix a few existing unit tests so that they terminate upon encountering unrecoverable failures. Fixes #21844 Change-Id: I6b4f6996cf5467113ef94f7b91a6933dbbc21839 Reviewed-on: https://go-review.googlesource.com/64770 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/html/template/template.go')
-rw-r--r--src/html/template/template.go5
1 files changed, 5 insertions, 0 deletions
diff --git a/src/html/template/template.go b/src/html/template/template.go
index 6a661bf6e5..d77aa3d7df 100644
--- a/src/html/template/template.go
+++ b/src/html/template/template.go
@@ -219,6 +219,11 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error
t.nameSpace.mu.Lock()
defer t.nameSpace.mu.Unlock()
+ for _, tmpl := range t.set {
+ if tmpl.Tree == tree {
+ return nil, fmt.Errorf("html/template: cannot add parse tree that template %q already references", tmpl.Name())
+ }
+ }
text, err := t.text.AddParseTree(name, tree)
if err != nil {
return nil, err