1
0
mirror of https://github.com/golang/go synced 2024-11-26 10:58:16 -07:00

text/template: error on range over send channel

template range require channel contains RecvDir
if recv on send only channel will raise an panic.

Fixes #43065

Change-Id: Ie0ea70ce60e074bf8c9f2378e07ef1d4c41dc38f
Reviewed-on: https://go-review.googlesource.com/c/go/+/276532
Trust: Meng Zhuo <mzh@golangcn.org>
Run-TryBot: Meng Zhuo <mzh@golangcn.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
Meng Zhuo 2020-12-10 09:52:52 +08:00 committed by Jonathan Amsterdam
parent 5a25a3fd1d
commit 3298300ddf
2 changed files with 17 additions and 0 deletions

View File

@ -373,6 +373,10 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
if val.IsNil() {
break
}
if val.Type().ChanDir() == reflect.SendDir {
s.errorf("range over send-only channel %v", val)
break
}
i := 0
for ; ; i++ {
elem, ok := val.Recv()

View File

@ -1697,3 +1697,16 @@ func TestIssue31810(t *testing.T) {
t.Errorf("%s got %q, expected %q", textCall, b.String(), "result")
}
}
// Issue 43065, range over send only channel
func TestIssue43065(t *testing.T) {
var b bytes.Buffer
tmp := Must(New("").Parse(`{{range .}}{{end}}`))
ch := make(chan<- int)
err := tmp.Execute(&b, ch)
if err == nil {
t.Error("expected err got nil")
} else if !strings.Contains(err.Error(), "range over send-only channel") {
t.Errorf("%s", err)
}
}