]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: don't crash piping to call with no arguments
authorIan Lance Taylor <iant@golang.org>
Thu, 14 Nov 2024 17:17:15 +0000 (09:17 -0800)
committerGopher Robot <gobot@golang.org>
Mon, 18 Nov 2024 16:24:22 +0000 (16:24 +0000)
Fixes #70341

Change-Id: I792b15d5e8d08c3762659fbcdfb3d620b59071ca
Reviewed-on: https://go-review.googlesource.com/c/go/+/628096
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Rob Pike <r@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Zxilly Chou <zhouxinyu1001@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/text/template/exec.go
src/text/template/exec_test.go

index 57f076e35f1c87b044b9bf891025495a0585da1b..ed6ae43671a1278f88118e92b316277f9a4fcc8e 100644 (file)
@@ -856,7 +856,13 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node
        // Special case for the "call" builtin.
        // Insert the name of the callee function as the first argument.
        if isBuiltin && name == "call" {
-               calleeName := args[0].String()
+               var calleeName string
+               if len(args) == 0 {
+                       // final must be present or we would have errored out above.
+                       calleeName = final.String()
+               } else {
+                       calleeName = args[0].String()
+               }
                argv = append([]reflect.Value{reflect.ValueOf(calleeName)}, argv...)
                fun = reflect.ValueOf(call)
        }
index cca53f4d72302ccfa39ac788e0f18359a1e78bdc..03ec9d759a0dd7f9cb062ce94ab6de90ad6d1a8b 100644 (file)
@@ -400,6 +400,9 @@ var execTests = []execTest{
        {"Interface Call", `{{stringer .S}}`, "foozle", map[string]any{"S": bytes.NewBufferString("foozle")}, true},
        {".ErrFunc", "{{call .ErrFunc}}", "bla", tVal, true},
        {"call nil", "{{call nil}}", "", tVal, false},
+       {"empty call", "{{call}}", "", tVal, false},
+       {"empty call after pipe valid", "{{.ErrFunc | call}}", "bla", tVal, true},
+       {"empty call after pipe invalid", "{{1 | call}}", "", tVal, false},
 
        // Erroneous function calls (check args).
        {".BinaryFuncTooFew", "{{call .BinaryFunc `1`}}", "", tVal, false},