]> Cypherpunks repositories - gostls13.git/commit
[release-branch.go1.12] text/template: error on method calls on nil interfaces
authorDaniel Martí <mvdan@mvdan.cc>
Sat, 9 Feb 2019 17:50:02 +0000 (17:50 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 13 Mar 2019 21:00:26 +0000 (21:00 +0000)
commitad8ebb9e85131aa1f37664b79f6f02415ba1a6e6
tree958e83158a89b8b56f4bf236c4b6581a2d1cd44a
parent6fc1242ea8f8bbacd88cb834ef61dbe9e84a8514
[release-branch.go1.12] text/template: error on method calls on nil interfaces

Trying to call a method on a nil interface is a panic in Go. For
example:

var stringer fmt.Stringer
println(stringer.String()) // nil pointer dereference

In https://golang.org/cl/143097 we started recovering panics encountered
during function and method calls. However, we didn't handle this case,
as text/template panics before evalCall is ever run.

In particular, reflect's MethodByName will panic if the receiver is of
interface kind and nil:

panic: reflect: Method on nil interface value

Simply add a check for that edge case, and have Template.Execute return
a helpful error. Note that Execute shouldn't just error if the interface
contains a typed nil, since we're able to find a method to call in that
case.

Finally, add regression tests for both the nil and typed nil interface
cases.

Fixes #30464.

Change-Id: Iffb21b40e14ba5fea0fcdd179cd80d1f23cabbab
Reviewed-on: https://go-review.googlesource.com/c/161761
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
(cherry picked from commit 15b4c71a912846530315c3e854feaaa9d0d54220)
Reviewed-on: https://go-review.googlesource.com/c/go/+/164457
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/text/template/exec.go
src/text/template/exec_test.go