From bcccad40202ba895d237d9d0a921b33bc2c5601f Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sun, 7 Oct 2012 09:26:59 +1100 Subject: [PATCH] text/template: fix nil crash on Templates Fixes #3872. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/6612060 --- src/pkg/text/template/exec_test.go | 5 +++++ src/pkg/text/template/template.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go index d79365107d3..683e9ac76b6 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -811,3 +811,8 @@ func TestTree(t *testing.T) { t.Errorf("expected %q got %q", expect, result) } } + +func TestExecuteOnNewTemplate(t *testing.T) { + // This is issue 3872. + _ = New("Name").Templates() +} diff --git a/src/pkg/text/template/template.go b/src/pkg/text/template/template.go index 82fc9e5e39d..a2b9062ad14 100644 --- a/src/pkg/text/template/template.go +++ b/src/pkg/text/template/template.go @@ -117,6 +117,9 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error // Templates returns a slice of the templates associated with t, including t // itself. func (t *Template) Templates() []*Template { + if t.common == nil { + return nil + } // Return a slice so we don't expose the map. m := make([]*Template, 0, len(t.tmpl)) for _, v := range t.tmpl {