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
Labels
CherryPickCandidate Used during the release process for point releases FrozenDueToAge
Milestone

Comments

@gopherbot
Copy link

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

@gopherbot please backport to 1.12

@gopherbot gopherbot added the CherryPickCandidate Used during the release process for point releases label Feb 28, 2019
@gopherbot gopherbot added this to the Go1.12.1 milestone Feb 28, 2019
@gopherbot
Copy link
Author

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
Member

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

@mvdan
Copy link
Member

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

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

@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.
Labels
CherryPickCandidate Used during the release process for point releases FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants