diff options
| author | Rob Pike <r@golang.org> | 2015-01-14 09:13:42 +1100 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2015-03-16 22:35:49 +0000 |
| commit | 5764befa5ace6e2dff623a79ba5a098d36fd1a86 (patch) | |
| tree | 5d293f8b0490f816f71191dc9a8c97c35910d854 /src | |
| parent | 10a98dd6d912783fbd9226615d16b526f2c9dd6c (diff) | |
| download | go-5764befa5ace6e2dff623a79ba5a098d36fd1a86.tar.xz | |
text/template: protect against explicit nil in field chains
An explicit nil in an expression like nil.Foo caused a panic
because the evaluator attempted to reflect on the nil.
A typeless nil like this cannot be used to do anything, so
just error out.
Fixes #9426
Change-Id: Icd2c9c7533dda742748bf161eced163991a12f54
Reviewed-on: https://go-review.googlesource.com/7643
Reviewed-by: David Symonds <dsymonds@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/text/template/exec.go | 7 | ||||
| -rw-r--r-- | src/text/template/exec_test.go | 2 |
2 files changed, 7 insertions, 2 deletions
diff --git a/src/text/template/exec.go b/src/text/template/exec.go index b00e10c7e4..faf31e3ede 100644 --- a/src/text/template/exec.go +++ b/src/text/template/exec.go @@ -418,11 +418,14 @@ func (s *state) evalFieldNode(dot reflect.Value, field *parse.FieldNode, args [] func (s *state) evalChainNode(dot reflect.Value, chain *parse.ChainNode, args []parse.Node, final reflect.Value) reflect.Value { s.at(chain) - // (pipe).Field1.Field2 has pipe as .Node, fields as .Field. Eval the pipeline, then the fields. - pipe := s.evalArg(dot, nil, chain.Node) if len(chain.Field) == 0 { s.errorf("internal error: no fields in evalChainNode") } + if chain.Node.Type() == parse.NodeNil { + s.errorf("indirection through explicit nil in %s", chain) + } + // (pipe).Field1.Field2 has pipe as .Node, fields as .Field. Eval the pipeline, then the fields. + pipe := s.evalArg(dot, nil, chain.Node) return s.evalFieldChain(dot, pipe, chain, chain.Field, args, final) } diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go index 69c213ed24..b1f778797b 100644 --- a/src/text/template/exec_test.go +++ b/src/text/template/exec_test.go @@ -527,6 +527,8 @@ var execTests = []execTest{ {"bug12XE", "{{printf `%T` 0XEE}}", "int", T{}, true}, // Chained nodes did not work as arguments. Issue 8473. {"bug13", "{{print (.Copy).I}}", "17", tVal, true}, + // Didn't protect against explicit nil in field chains. + {"bug14", "{{nil.True}}", "", tVal, false}, } func zeroArgs() string { |
