Skip to content

text/template: Panic when an undefined variable was accessed #54149

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
YoitoFes opened this issue Jul 31, 2022 · 4 comments
Closed

text/template: Panic when an undefined variable was accessed #54149

YoitoFes opened this issue Jul 31, 2022 · 4 comments

Comments

@YoitoFes
Copy link

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

$ go version
go version go1.18.4 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Naoto\AppData\Local\go-build
set GOENV=C:\Users\Naoto\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\Users\Naoto\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\Users\Naoto\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=E:\Documents\golang-test\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=D:\AppData\Local\Temp\go-build3068758331=/tmp/go-build -gno-record-gcc-switches

What did you do?

Accessed undefined variable in a template as follows:

Runnable program

package main

import (
	"fmt"
	"os"
	"text/template"
)

func main() {
	tmpl, _ := template.New("test_template").Parse("Hello {{$val}}")
	err := tmpl.Execute(os.Stdout, "test")
	if err != nil {
		fmt.Println(err)
	}
}

go.dev/play link

What did you expect to see?

Got non-nil err

What did you see instead?

Got a panic

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x4e0d8c]

goroutine 1 [running]:
text/template.errRecover(0xc00004de98)
	/usr/local/go-faketime/src/text/template/exec.go:164 +0x15b
panic({0x501d20, 0x5f65c0})
	/usr/local/go-faketime/src/runtime/panic.go:838 +0x207
text/template.(*Template).execute(0x0, {0x547168?, 0xc00000e018?}, {0x4fb840?, 0x5468a0?})
	/usr/local/go-faketime/src/text/template/exec.go:215 +0x22c
text/template.(*Template).Execute(...)
	/usr/local/go-faketime/src/text/template/exec.go:201
main.main()
	/tmp/sandbox2346080729/prog.go:13 +0x11c
@seankhliao
Copy link
Member

Don't ignore errors.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2022
@YoitoFes YoitoFes changed the title text/template: text/template: Panic when an undefined variable was accessed Jul 31, 2022
@YoitoFes
Copy link
Author

YoitoFes commented Jul 31, 2022

@seankhliao

Don't ignore errors.

tmpl.Execute(os.Stdout, "test") panics instead of returning error. IMO tmpl.Execute() should return error.

@ianlancetaylor
Copy link
Contributor

@YoitoFes You are ignoring the error from template.New. Don't do that.

@ianlancetaylor
Copy link
Contributor

It is not a goal for the standard library to never panic. It will panic in some cases when called with invalid arguments.

@golang golang locked and limited conversation to collaborators Jul 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants