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: failure to parse {{printf "hi"-}} #17974

Closed
DrGo opened this issue Nov 18, 2016 · 3 comments
Closed

text/template: failure to parse {{printf "hi"-}} #17974

DrGo opened this issue Nov 18, 2016 · 3 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@DrGo
Copy link
Contributor

DrGo commented Nov 18, 2016

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

go1.7 darwin/amd64

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

GOARCH="amd64"
GOOS="darwin"

What did you do?

Parse and execute this template
Research Grants {{range Block "Research Funding History"}} {{.Emit "Funding Start Date, Funding End Date, Funding Title"-}} {{range .SubSection "Funding Sources" }} {{.Emit "Program Name, Funding Organization" -}} {{end}} {{end}}

What did you expect to see?

white space trimmed

What did you see instead?

SEGV (see below) in several scenarios eg this crashes
{{.Emit "Funding Start Date, Funding End Date, Funding Title"-}}
but not this one
{{.Emit "Funding Start Date, Funding End Date, Funding Title" -}}
Notice space before -}}

Also, SEGV when adding - to {{end}} eg {{-end}} or {{end-}}

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=0x20 pc=0xed934]

goroutine 3 [running]:
panic(0x1a25a0, 0xc42000a090)
/Users/salah/go/src/runtime/panic.go:500 +0x1a1
testing.tRunner.func1(0xc4200a4180)
/Users/salah/go/src/testing/testing.go:579 +0x25d
panic(0x1a25a0, 0xc42000a090)
/Users/salah/go/src/runtime/panic.go:458 +0x243
html/template.(*Template).escape(0x0, 0x0, 0x0)
/Users/salah/go/src/html/template/template.go:79 +0x44
html/template.(*Template).Execute(0x0, 0x2a7840, 0xc420086008, 0x18e3e0, 0xc4200740c0, 0xc42000b160, 0xc420124200)
/Users/salah/go/src/html/template/template.go:101 +0x2f
salah/CCV/template.TestGoTemplate(0xc4200a4180)
/Users/salah/Dropbox/code/go/src/salah/CCV/template/template_test.go:117 +0x45e
testing.tRunner(0xc4200a4180, 0x1ecae0)

Edit:
managed to replicate the error here https://play.golang.org/p/V6VoOm_tWR
the error occurs with Output 1: {{title . | printf "%q"-}}
but not with this one "{{23 -}} < {{ "test"-}}"

@DrGo DrGo changed the title html/template SEGV using white space trimmers {{- html/template panic using white space trimmers {{- Nov 18, 2016
@DrGo DrGo changed the title html/template panic using white space trimmers {{- html/template panic using white space trimmers {{- and -}} Nov 18, 2016
@robpike robpike self-assigned this Nov 18, 2016
@robpike robpike added this to the Go1.8 milestone Nov 18, 2016
@quentinmit quentinmit changed the title html/template panic using white space trimmers {{- and -}} html/template: panic using white space trimmers {{- and -}} Nov 18, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Nov 18, 2016
@robpike
Copy link
Contributor

robpike commented Nov 23, 2016

The panic is because the template fails to parse and so the user is attempting to execute a nil template.

The real question is why it is failing to parse.

This reproduces the problem: https://play.golang.org/p/nIhkYi0gAe

package main

import (
	"fmt"
	"text/template"
)

func main() {
	t, err := template.New("x").Parse(`{{printf "hi"-}}`)
	fmt.Println(t, err)
}

Updating the issue title.

@robpike robpike changed the title html/template: panic using white space trimmers {{- and -}} text/template: failure to parse {{printf "hi"-}} Nov 23, 2016
@robpike
Copy link
Contributor

robpike commented Nov 23, 2016

And now we resolve this issue, as the template text is illegal. As clearly stated in the documentation,

"the ASCII space must be present; "{{-3}}" parses as an action containing the number -3."

If we put a space before the -, all is well. https://play.golang.org/p/tvplRjhFh6

@robpike robpike closed this as completed Nov 23, 2016
@DrGo
Copy link
Contributor Author

DrGo commented Nov 26, 2016

Thanks Rob and some for the trouble!

@golang golang locked and limited conversation to collaborators Nov 26, 2017
@rsc rsc unassigned robpike Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants