diff options
| author | Daniel Martí <mvdan@mvdan.cc> | 2019-02-09 17:50:02 +0000 |
|---|---|---|
| committer | Daniel Martí <mvdan@mvdan.cc> | 2019-02-26 18:05:09 +0000 |
| commit | 15b4c71a912846530315c3e854feaaa9d0d54220 (patch) | |
| tree | 3d5cb245298286912f53c500c3c70abfa945239d /src/encoding/json/example_test.go | |
| parent | acf786f4fb08bd75e4f40b8e89e60878b1f47de3 (diff) | |
| download | go-15b4c71a912846530315c3e854feaaa9d0d54220.tar.xz | |
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>
Diffstat (limited to 'src/encoding/json/example_test.go')
0 files changed, 0 insertions, 0 deletions
