Skip to content

Commit

Permalink
text/template: avoid index-out-of-range panic when accessing args[0]
Browse files Browse the repository at this point in the history
Fixes golang#70341

Change-Id: I3df0175929b4aed76522ef36aecfa924f3883d9e
  • Loading branch information
callthingsoff committed Nov 14, 2024
1 parent 8e71428 commit 88d346a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/text/template/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 13 additions & 6 deletions src/text/template/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand Down

0 comments on commit 88d346a

Please sign in to comment.