diff options
| author | Josh Bleecher Snyder <josharian@gmail.com> | 2013-09-17 14:19:44 +1000 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2013-09-17 14:19:44 +1000 |
| commit | eeb758546e10b33be161e76b3c3290dbb7a70a87 (patch) | |
| tree | ec2aac03c9a40ab8fba44c693e3ca6d5e8b1d6a9 /src/pkg/text | |
| parent | 8d5ec52e6cf3a259b9054ee3c3621834f2491860 (diff) | |
| download | go-eeb758546e10b33be161e76b3c3290dbb7a70a87.tar.xz | |
text/template/parse, html/template: copy Tree.text during html template clone
The root cause of the panic reported in https://code.google.com/p/go/issues/detail?id=5980
is that parse's Tree.Text wasn't being copied during the clone.
Fix this by adding and using a Copy method for parse.Tree.
Fixes #5980.
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/12420044
Diffstat (limited to 'src/pkg/text')
| -rw-r--r-- | src/pkg/text/template/parse/parse.go | 13 | ||||
| -rw-r--r-- | src/pkg/text/template/parse/parse_test.go | 16 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/pkg/text/template/parse/parse.go b/src/pkg/text/template/parse/parse.go index be83e77cf5..34112fb7b3 100644 --- a/src/pkg/text/template/parse/parse.go +++ b/src/pkg/text/template/parse/parse.go @@ -30,6 +30,19 @@ type Tree struct { vars []string // variables defined at the moment. } +// Copy returns a copy of the Tree. Any parsing state is discarded. +func (t *Tree) Copy() *Tree { + if t == nil { + return nil + } + return &Tree{ + Name: t.Name, + ParseName: t.ParseName, + Root: t.Root.CopyList(), + text: t.text, + } +} + // Parse returns a map from template name to parse.Tree, created by parsing the // templates described in the argument string. The top-level template will be // given the specified name. If an error is encountered, parsing stops and an diff --git a/src/pkg/text/template/parse/parse_test.go b/src/pkg/text/template/parse/parse_test.go index 049e65c7d3..ba1a18ec54 100644 --- a/src/pkg/text/template/parse/parse_test.go +++ b/src/pkg/text/template/parse/parse_test.go @@ -332,6 +332,22 @@ func TestIsEmpty(t *testing.T) { } } +func TestErrorContextWithTreeCopy(t *testing.T) { + tree, err := New("root").Parse("{{if true}}{{end}}", "", "", make(map[string]*Tree), nil) + if err != nil { + t.Fatalf("unexpected tree parse failure: %v", err) + } + treeCopy := tree.Copy() + wantLocation, wantContext := tree.ErrorContext(tree.Root.Nodes[0]) + gotLocation, gotContext := treeCopy.ErrorContext(treeCopy.Root.Nodes[0]) + if wantLocation != gotLocation { + t.Errorf("wrong error location want %q got %q", wantLocation, gotLocation) + } + if wantContext != gotContext { + t.Errorf("wrong error location want %q got %q", wantContext, gotContext) + } +} + // All failures, and the result is a string that must appear in the error message. var errorTests = []parseTest{ // Check line numbers are accurate. |
