]> Cypherpunks repositories - gostls13.git/commit
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)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 26 Feb 2019 18:05:09 +0000 (18:05 +0000)
commit15b4c71a912846530315c3e854feaaa9d0d54220
tree3d5cb245298286912f53c500c3c70abfa945239d
parentacf786f4fb08bd75e4f40b8e89e60878b1f47de3
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 #30143.

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>
src/text/template/exec.go
src/text/template/exec_test.go