-
Notifications
You must be signed in to change notification settings - Fork 18k
text/template: panics on method on nil interface value [1.12 backport] #30464
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
Comments
Change https://golang.org/cl/164457 mentions this issue: |
@mvdan - there isn't a reason provided in the gopherbot message. Would you mind providing one for this backport? |
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? |
…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í <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> (cherry picked from commit 15b4c71) Reviewed-on: https://go-review.googlesource.com/c/go/+/164457 Reviewed-by: Brad Fitzpatrick <[email protected]>
Closed by merging ad8ebb9 to release-branch.go1.12. |
@mvdan requested issue #30143 to be considered for backport to the next 1.12 minor release.
The text was updated successfully, but these errors were encountered: