From 88d346a7ca67659fb91b0e5d986b0de74a6127b8 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Fri, 15 Nov 2024 00:30:23 +0800 Subject: [PATCH] text/template: avoid index-out-of-range panic when accessing args[0] Fixes #70341 Change-Id: I3df0175929b4aed76522ef36aecfa924f3883d9e --- src/text/template/exec.go | 2 +- src/text/template/exec_test.go | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/text/template/exec.go b/src/text/template/exec.go index 57f076e35f1..2eb54b24e0f 100644 --- a/src/text/template/exec.go +++ b/src/text/template/exec.go @@ -855,7 +855,7 @@ 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" { + if len(args) > 0 && isBuiltin && name == "call" { calleeName := args[0].String() argv = append([]reflect.Value{reflect.ValueOf(calleeName)}, argv...) fun = reflect.ValueOf(call) diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go index cca53f4d723..85f4cdf3b20 100644 --- a/src/text/template/exec_test.go +++ b/src/text/template/exec_test.go @@ -1779,12 +1779,19 @@ func TestFunctionCheckDuringCall(t *testing.T) { input string data any wantErr string - }{{ - name: "call nothing", - input: `{{call}}`, - data: tVal, - wantErr: "wrong number of args for call: want at least 1 got 0", - }, + }{ + { + name: "call with no arguments", + input: `{{ 1 | call }}`, + data: tVal, + wantErr: "error calling call: unreachable", + }, + { + name: "call nothing", + input: `{{call}}`, + data: tVal, + wantErr: "wrong number of args for call: want at least 1 got 0", + }, { name: "call non-function", input: "{{call .True}}",