diff options
author | Rob Pike <r@golang.org> | 2012-04-03 11:44:52 +1000 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2012-04-03 11:44:52 +1000 |
commit | efcd0d5bd82a9fd7d3847b459d61b67f962437c9 (patch) | |
tree | 61fb2257a98d1f412a2c6b15b5401259b6ad0fad | |
parent | 7510885c4dc552e343b8a49a8e51210534ef3ba2 (diff) | |
download | go-efcd0d5bd82a9fd7d3847b459d61b67f962437c9.tar.gz go-efcd0d5bd82a9fd7d3847b459d61b67f962437c9.zip |
[release-branch.go1] text/template: pipelined arg was not typechecked
««« backport c007925a75c6
text/template: pipelined arg was not typechecked
Without this fix, an erroneous template causes a panic; should be caught safely.
The bug did not affect correct templates.
Fixes #3267.
R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/5900065
»»»
-rw-r--r-- | src/pkg/text/template/exec.go | 7 | ||||
-rw-r--r-- | src/pkg/text/template/exec_test.go | 8 |
2 files changed, 14 insertions, 1 deletions
diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go index 9a720cf43e..feb434a3be 100644 --- a/src/pkg/text/template/exec.go +++ b/src/pkg/text/template/exec.go @@ -491,7 +491,11 @@ func (s *state) evalCall(dot, fun reflect.Value, name string, args []parse.Node, } // Add final value if necessary. if final.IsValid() { - argv[i] = final + t := typ.In(typ.NumIn() - 1) + if typ.IsVariadic() { + t = t.Elem() + } + argv[i] = s.validateType(final, t) } result := fun.Call(argv) // If we have an error that is not nil, stop execution and return that error to the caller. @@ -507,6 +511,7 @@ func (s *state) validateType(value reflect.Value, typ reflect.Type) reflect.Valu switch typ.Kind() { case reflect.Interface, reflect.Ptr, reflect.Chan, reflect.Map, reflect.Slice, reflect.Func: // An untyped nil interface{}. Accept as a proper nil value. + // TODO: Can we delete the other types in this list? Should we? value = reflect.Zero(typ) default: s.errorf("invalid value; expected %s", typ) diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go index 5446027ff7..37d25f470c 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -470,6 +470,9 @@ var execTests = []execTest{ {"bug7a", "{{3 2}}", "", tVal, false}, {"bug7b", "{{$x := 1}}{{$x 2}}", "", tVal, false}, {"bug7c", "{{$x := 1}}{{3 | $x}}", "", tVal, false}, + // Pipelined arg was not being type-checked. + {"bug8a", "{{3|oneArg}}", "", tVal, false}, + {"bug8b", "{{4|dddArg 3}}", "", tVal, false}, } func zeroArgs() string { @@ -480,6 +483,10 @@ func oneArg(a string) string { return "oneArg=" + a } +func dddArg(a int, b ...string) string { + return fmt.Sprintln(a, b) +} + // count returns a channel that will deliver n sequential 1-letter strings starting at "a" func count(n int) chan string { if n == 0 { @@ -504,6 +511,7 @@ func testExecute(execTests []execTest, template *Template, t *testing.T) { b := new(bytes.Buffer) funcs := FuncMap{ "count": count, + "dddArg": dddArg, "oneArg": oneArg, "typeOf": typeOf, "vfunc": vfunc, |