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

html/template: Incorrect append values may result in incomplete HTML output #69076

Closed
cuishuang opened this issue Aug 27, 2024 · 2 comments
Closed

Comments

@cuishuang
Copy link
Contributor

Go version

go version devel go1.24-b2f3a427dd Fri Aug 23 00:53:47 2024 +0000 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/xxxx/Library/Caches/go-build'
GOENV='/Users/xxxx/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/xxxx/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/xxxx/go'
GOPRIVATE=''
GOPROXY=''
GOROOT='/Users/xxxx/go'
GOSUMDB='off'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/xxxx/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='devel go1.24-b2f3a427dd Fri Aug 23 00:53:47 2024 +0000'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/xxxx/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='0'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/9t/839s3jmj73bcgyp5x_xh3gw00000gn/T/go-build550188458=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Execute the following code:

https://go.dev/play/p/RYn3g_NWx_J

package main

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

type Item struct {
	X, Y, Z bool
	Name    string
}

func main() {
	tmpl := template.Must(template.New("test").Parse(`
{{range $index, $item := .Items}}
Item {{$index}}: {{$item.Name}}
{{if $item.X}}{{break}}{{end}}
<a
{{if $item.Y}}{{continue}}{{end}}
>
{{if $item.Z}}{{continue}}{{end}}
{{$item.Name}}
</a>
{{end}}
`))

	data := struct {
		Items []Item
	}{
		Items: []Item{
			{X: false, Y: false, Z: false, Name: "First"},
			{X: false, Y: true, Z: false, Name: "Second"},
			{X: false, Y: false, Z: true, Name: "Third"},
			{X: true, Y: false, Z: false, Name: "Fourth"},
			{X: false, Y: false, Z: false, Name: "Fifth"},
		},
	}

	fmt.Println("Template output:")
	err := tmpl.Execute(os.Stdout, data)
	if err != nil {
		fmt.Println("Error:", err)
	}
}

What did you see happen?

The following code will not throw an error, but will output incomplete HTML content:

Template output:


Item 0: First

<a

>

First
</a>

Item 1: Second

<a

Item 2: Third

<a

>

Item 3: Fourth

What did you expect to see?

Throwing an error.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/607736 mentions this issue: html/template: fix the issue of incomplete HTML output due to incorrect append values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@gopherbot @cuishuang @gabyhelp and others