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
Assignees
Milestone

Comments

@DrGo
Copy link

@DrGo 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 label Nov 18, 2016
@robpike
Copy link
Contributor

@robpike 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 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 Nov 23, 2016
@DrGo
Copy link
Author

@DrGo DrGo commented Nov 26, 2016

Thanks Rob and some for the trouble!

@golang golang locked and limited conversation to collaborators Nov 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.