diff options
| author | Rob Pike <r@golang.org> | 2015-03-20 10:47:52 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2015-03-20 19:57:45 +0000 |
| commit | 11dba2ec2d8fcedc1da0103925c254586ef51120 (patch) | |
| tree | 1d66a39e3293847dae8d3c52d7237fce58338318 /src/html | |
| parent | 3f12d271335abc9d49d419247528cf286a04f9a6 (diff) | |
| download | go-11dba2ec2d8fcedc1da0103925c254586ef51120.tar.xz | |
html/template: fix crash when escaping incomplete template
text/template turned this into an error but html/template crashed.
Refactor text/template.Execute to export a new function,
text/template.DefinedTemplates, so html/template can get the same
helpful error message in this case, and invoke it when there is no
definition for a template being escaped.
Fixes #10204.
Change-Id: I1d04e9e7ebca829bc08509caeb65e75da969711f
Reviewed-on: https://go-review.googlesource.com/7855
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/html')
| -rw-r--r-- | src/html/template/escape_test.go | 15 | ||||
| -rw-r--r-- | src/html/template/template.go | 3 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/html/template/escape_test.go b/src/html/template/escape_test.go index ef7b877484..9c9502a617 100644 --- a/src/html/template/escape_test.go +++ b/src/html/template/escape_test.go @@ -1686,6 +1686,21 @@ func TestPipeToMethodIsEscaped(t *testing.T) { } } +// Unlike text/template, html/template crashed if given an incomplete +// template, that is, a template that had been named but not given any content. +// This is issue #10204. +func TestErrorOnUndefined(t *testing.T) { + tmpl := New("undefined") + + err := tmpl.Execute(nil, nil) + if err == nil { + t.Error("expected error") + } + if !strings.Contains(err.Error(), "incomplete") { + t.Errorf("expected error about incomplete template; got %s", err) + } +} + func BenchmarkEscapedExecute(b *testing.B) { tmpl := Must(New("t").Parse(`<a onclick="alert('{{.}}')">{{.}}</a>`)) var buf bytes.Buffer diff --git a/src/html/template/template.go b/src/html/template/template.go index ce6170105c..64c0041c9c 100644 --- a/src/html/template/template.go +++ b/src/html/template/template.go @@ -56,6 +56,9 @@ func (t *Template) escape() error { t.nameSpace.mu.Lock() defer t.nameSpace.mu.Unlock() if t.escapeErr == nil { + if t.Tree == nil { + return fmt.Errorf("template: %q is an incomplete or empty template%s", t.Name(), t.text.DefinedTemplates()) + } if err := escapeTemplate(t, t.text.Root, t.Name()); err != nil { return err } |
