Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text/template: panics on method on nil interface value [1.12 backport] #30464

Closed
gopherbot opened this issue Feb 28, 2019 · 4 comments
Closed

text/template: panics on method on nil interface value [1.12 backport] #30464

gopherbot opened this issue Feb 28, 2019 · 4 comments

Comments

@gopherbot
Copy link

@gopherbot gopherbot commented Feb 28, 2019

@mvdan requested issue #30143 to be considered for backport to the next 1.12 minor release.

@gopherbot please backport to 1.12

@gopherbot
Copy link
Author

@gopherbot gopherbot commented Feb 28, 2019

Change https://golang.org/cl/164457 mentions this issue: [release-branch.go1.12] text/template: error on method calls on nil interfaces

@julieqiu
Copy link
Contributor

@julieqiu julieqiu commented Mar 12, 2019

@mvdan - there isn't a reason provided in the gopherbot message. Would you mind providing one for this backport?

@mvdan
Copy link
Member

@mvdan mvdan commented Mar 13, 2019

We fixed one set of panics when making template calls, but forgot some others. This makes 1.12 more consistent in handling panics during template calls.

Is there a convention to add the reason for the backport in the backport issue directly?

gopherbot pushed a commit that referenced this issue Mar 13, 2019
…nterfaces

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 15b4c71)
Reviewed-on: https://go-review.googlesource.com/c/go/+/164457
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@gopherbot
Copy link
Author

@gopherbot gopherbot commented Mar 13, 2019

Closed by merging ad8ebb9 to release-branch.go1.12.

@gopherbot gopherbot closed this Mar 13, 2019
@golang golang locked and limited conversation to collaborators Mar 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.