Skip to content

text/template: panic: indirection through nil pointer to embedded struct #48215

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

Closed
rhcarvalho opened this issue Sep 6, 2021 · 3 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@rhcarvalho
Copy link
Contributor

What version of Go are you using (go version)?

$ go version
go version go1.17 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

This is similar to #30143 and #28242.

While working with a template, I was getting panics that were hard to debug because there is no contextual information available to pinpoint where is the problem in the template/data.

I was able to reduce template to reproduce the panic:

package main

import (
	"io"
	"testing"
	"text/template"
)

func TestTemplateExecute(t *testing.T) {
	type A struct {
		S string
	}
	type B struct {
		*A
	}
	tmpl := template.Must(template.New("").Parse(`{{ .S }}`))
	// tmpl.Execute panics
	if err := tmpl.Execute(io.Discard, B{}); err != nil {
		t.Fatal(err)
	}
}

https://play.golang.org/p/0l9Ud0i7ZzH

What did you expect to see?

I expected an error with contextual information, similar to when the nil pointer is not an embedded field:

template: :1:5: executing "" at <.A.S>: nil pointer evaluating *main.A.S
Test Function
package main

import (
	"io"
	"testing"
	"text/template"
)

func TestTemplateExecute(t *testing.T) {
	type A struct {
		S string
	}
	type B struct {
		A *A
	}
	tmpl := template.Must(template.New("").Parse(`{{ .A.S }}`))
	if err := tmpl.Execute(io.Discard, B{}); err != nil {
		t.Fatal(err)
	}
}

What did you see instead?

panic: reflect: indirection through nil pointer to embedded struct
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 6, 2021
@seankhliao
Copy link
Member

cc @robpike

@robpike
Copy link
Contributor

robpike commented Sep 6, 2021

Finally figured this out and have a fix. I'll send it out once I've convinced it's the right fix and track down all the existing reports of this.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/357963 mentions this issue: text/template: use reflect.Value.FieldByIndexErr

@rsc rsc unassigned robpike Jun 23, 2022
@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants