From 013b899bc808f424c4054a5d97e0520e8c01e374 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Tue, 24 Nov 2026 21:53:41 +0100 Subject: [PATCH 1/3] Report too few builtin arguments in unchecked calls The checker does not check arguments of a call on an unknown value, so fn().N(join()) panicked with index out of range or stack underflow. Throw an invalid number of arguments error instead. Based on #943. Fixes #941 --- builtin/builtin.go | 3 +++ builtin/builtin_test.go | 27 +++++++++++++++++++++++++++ compiler/compiler.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/builtin/builtin.go b/builtin/builtin.go index f86b7efd9..10a03ad9e 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -574,6 +574,9 @@ var Builtins = []*Function{ { Name: "date", Func: func(args ...any) (any, error) { + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for date (expected at least 1, got 0)") + } tz, ok := args[0].(*time.Location) if ok { args = args[1:] diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 6418654bd..4d4eb9095 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -375,6 +375,33 @@ func TestBuiltin_errors(t *testing.T) { } } +// The checker does not check arguments of a call on an unknown value, so a +// builtin with a wrong number of arguments there must fail with a clean error. +func TestBuiltin_wrong_number_of_arguments_unchecked(t *testing.T) { + env := map[string]any{"fn": func() any { return nil }} + tests := []struct { + input string + err string + }{ + {`fn().N(join())`, "invalid number of arguments for join (expected at least 1, got 0)"}, + {`fn().N(indexOf("a"))`, "invalid number of arguments for indexOf (expected 2, got 1)"}, + {`fn().N(repeat("a"))`, "invalid number of arguments for repeat (expected 2, got 1)"}, + {`fn().N(toJSON())`, "invalid number of arguments for toJSON (expected 1, got 0)"}, + {`fn().N(date())`, "invalid number of arguments for date (expected at least 1, got 0)"}, + {`fn().N(upper())`, "invalid number of arguments for upper (expected 1, got 0)"}, + } + for _, test := range tests { + t.Run(test.input, func(t *testing.T) { + program, err := expr.Compile(test.input, expr.Env(env)) + require.NoError(t, err) + + _, err = expr.Run(program, env) + require.Error(t, err) + assert.Equal(t, test.err, strings.Split(err.Error(), " (1:")[0]) + }) + } +} + func TestBuiltin_env_not_callable(t *testing.T) { code := `$env(''matches'i'?t:get().UTC())` env := map[string]any{"t": 1} diff --git a/compiler/compiler.go b/compiler/compiler.go index d63a646ff..e917ac43d 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1165,6 +1165,11 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) { if id, ok := builtin.Index[node.Name]; ok { f := builtin.Builtins[id] + if err := builtinArgsError(f, len(node.Arguments)); err != nil { + c.emit(OpPush, c.addConstant(err)) + c.emit(OpThrow) + return + } for i, arg := range node.Arguments { c.compile(arg) argType := arg.Type() @@ -1200,6 +1205,41 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) { panic(fmt.Sprintf("unknown builtin %v", node.Name)) } +// builtinArgsError returns an error if builtin f gets too few arguments. The +// checker reports a wrong number of arguments at compile time, but it does not +// check the arguments of a call on an unknown value, as in `x.Method(join())`. +// Such a call fails at run time with this error instead of indexing past the +// arguments or underflowing the stack. +func builtinArgsError(f *builtin.Function, n int) error { + if f.Fast != nil { + if n == 0 { + return fmt.Errorf("invalid number of arguments for %s (expected 1, got 0)", f.Name) + } + return nil + } + least, most := -1, -1 + for _, t := range f.Types { + in := t.NumIn() + if t.IsVariadic() { + in-- + most = math.MaxInt + } + if least < 0 || in < least { + least = in + } + if in > most { + most = in + } + } + if n >= least { + return nil + } + if least == most { + return fmt.Errorf("invalid number of arguments for %s (expected %d, got %d)", f.Name, least, n) + } + return fmt.Errorf("invalid number of arguments for %s (expected at least %d, got %d)", f.Name, least, n) +} + func (c *compiler) emitCond(body func()) { noop := c.emit(OpJumpIfFalse, placeholder) c.emit(OpPop) From af9ab8277900251d8dea9bd77d653de8528f313a Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Tue, 24 Nov 2026 22:05:17 +0100 Subject: [PATCH 2/3] Fix date with only a location and Fast builtin extra arguments date(timezone("UTC")) passed the checker and panicked at run time; reject it in Validate and guard Func. Extra arguments of a Fast builtin in an unchecked call stayed on the stack and leaked into the enclosing expression; pop them. Name exact argument ranges in the arity error. --- builtin/builtin.go | 6 ++++++ builtin/builtin_test.go | 27 ++++++++++++++++++++++++++- compiler/compiler.go | 32 ++++++++++++++++++++++++-------- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/builtin/builtin.go b/builtin/builtin.go index 10a03ad9e..3f2537704 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -580,6 +580,9 @@ var Builtins = []*Function{ tz, ok := args[0].(*time.Location) if ok { args = args[1:] + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for date (expected at least 2, got 1)") + } } date := args[0].(string) @@ -634,6 +637,9 @@ var Builtins = []*Function{ } if args[0] != nil && args[0].AssignableTo(locationType) { args = args[1:] + if len(args) == 0 { + return anyType, fmt.Errorf("invalid number of arguments (expected at least 2, got 1)") + } } if len(args) > 3 { return anyType, fmt.Errorf("invalid number of arguments (expected at most 3, got %d)", len(args)) diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 4d4eb9095..1cc680b07 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -383,7 +383,7 @@ func TestBuiltin_wrong_number_of_arguments_unchecked(t *testing.T) { input string err string }{ - {`fn().N(join())`, "invalid number of arguments for join (expected at least 1, got 0)"}, + {`fn().N(join())`, "invalid number of arguments for join (expected 1 or 2, got 0)"}, {`fn().N(indexOf("a"))`, "invalid number of arguments for indexOf (expected 2, got 1)"}, {`fn().N(repeat("a"))`, "invalid number of arguments for repeat (expected 2, got 1)"}, {`fn().N(toJSON())`, "invalid number of arguments for toJSON (expected 1, got 0)"}, @@ -402,6 +402,31 @@ func TestBuiltin_wrong_number_of_arguments_unchecked(t *testing.T) { } } +// Extra arguments of a Fast builtin in an unchecked call must not stay on the +// stack and leak into the surrounding expression. +func TestBuiltin_fast_extra_arguments_unchecked(t *testing.T) { + env := map[string]any{ + "x": "X", + "fn": func() any { return identity{} }, + } + program, err := expr.Compile(`x + fn().N(upper("a", "b"))`, expr.Env(env)) + require.NoError(t, err) + + out, err := expr.Run(program, env) + require.NoError(t, err) + assert.Equal(t, "XB", out) +} + +type identity struct{} + +func (identity) N(v any) any { return v } + +func TestBuiltin_date_location_only(t *testing.T) { + _, err := expr.Compile(`date(timezone("UTC"))`) + require.Error(t, err) + assert.Contains(t, err.Error(), "invalid number of arguments (expected at least 2, got 1)") +} + func TestBuiltin_env_not_callable(t *testing.T) { code := `$env(''matches'i'?t:get().UTC())` env := map[string]any{"t": 1} diff --git a/compiler/compiler.go b/compiler/compiler.go index e917ac43d..32cf83c49 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1172,6 +1172,13 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) { } for i, arg := range node.Arguments { c.compile(arg) + if f.Fast != nil && i < len(node.Arguments)-1 { + // A Fast builtin takes one argument. Extra arguments can only + // reach here unchecked (see builtinArgsError); they are evaluated + // and dropped so they do not stay on the stack. + c.emit(OpPop) + continue + } argType := arg.Type() if argType.Kind() == reflect.Ptr || arg.Nature().IsUnknown(c.ntCache) { if f.Deref == nil { @@ -1205,11 +1212,12 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) { panic(fmt.Sprintf("unknown builtin %v", node.Name)) } -// builtinArgsError returns an error if builtin f gets too few arguments. The -// checker reports a wrong number of arguments at compile time, but it does not -// check the arguments of a call on an unknown value, as in `x.Method(join())`. -// Such a call fails at run time with this error instead of indexing past the -// arguments or underflowing the stack. +// builtinArgsError returns an error if builtin f gets fewer arguments than +// its Types allow (or none, for a Fast builtin). The checker reports a wrong +// number of arguments at compile time, but it does not check the arguments of a +// call on an unknown value, as in `x.Method(join())`. Such a call fails at run +// time with this error instead of indexing past the arguments or underflowing +// the stack. Builtins with only a Validate function check their own arguments. func builtinArgsError(f *builtin.Function, n int) error { if f.Fast != nil { if n == 0 { @@ -1234,10 +1242,18 @@ func builtinArgsError(f *builtin.Function, n int) error { if n >= least { return nil } - if least == most { - return fmt.Errorf("invalid number of arguments for %s (expected %d, got %d)", f.Name, least, n) + var expected string + switch { + case most == math.MaxInt: + expected = fmt.Sprintf("at least %d", least) + case least == most: + expected = fmt.Sprintf("%d", least) + case most == least+1: + expected = fmt.Sprintf("%d or %d", least, most) + default: + expected = fmt.Sprintf("%d to %d", least, most) } - return fmt.Errorf("invalid number of arguments for %s (expected at least %d, got %d)", f.Name, least, n) + return fmt.Errorf("invalid number of arguments for %s (expected %s, got %d)", f.Name, expected, n) } func (c *compiler) emitCond(body func()) { From 904385c7ac16d9a66087048702f68853b3bbced2 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Wed, 25 Nov 2026 09:21:22 +0100 Subject: [PATCH 3/3] Check missing arguments of first, last, max, min, mean and median --- builtin/builtin.go | 18 ++++++++++++++++++ builtin/builtin_test.go | 6 ++++++ compiler/compiler.go | 1 + 3 files changed, 25 insertions(+) diff --git a/builtin/builtin.go b/builtin/builtin.go index 3f2537704..f762181ab 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -443,6 +443,9 @@ var Builtins = []*Function{ { Name: "max", Func: func(args ...any) (any, error) { + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for max (expected at least 1, got 0)") + } return minMax("max", runtime.Less, 0, args...) }, Validate: func(args []reflect.Type) (reflect.Type, error) { @@ -452,6 +455,9 @@ var Builtins = []*Function{ { Name: "min", Func: func(args ...any) (any, error) { + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for min (expected at least 1, got 0)") + } return minMax("min", runtime.More, 0, args...) }, Validate: func(args []reflect.Type) (reflect.Type, error) { @@ -461,6 +467,9 @@ var Builtins = []*Function{ { Name: "mean", Func: func(args ...any) (any, error) { + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for mean (expected at least 1, got 0)") + } count, sum, err := mean(0, args...) if err != nil { return nil, err @@ -477,6 +486,9 @@ var Builtins = []*Function{ { Name: "median", Func: func(args ...any) (any, error) { + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for median (expected at least 1, got 0)") + } values, err := median(0, args...) if err != nil { return nil, err @@ -667,6 +679,9 @@ var Builtins = []*Function{ { Name: "first", Func: func(args ...any) (any, error) { + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for first (expected 1, got 0)") + } defer func() { if r := recover(); r != nil { return @@ -690,6 +705,9 @@ var Builtins = []*Function{ { Name: "last", Func: func(args ...any) (any, error) { + if len(args) == 0 { + return nil, fmt.Errorf("invalid number of arguments for last (expected 1, got 0)") + } defer func() { if r := recover(); r != nil { return diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 1cc680b07..5a0db9fe9 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -389,6 +389,12 @@ func TestBuiltin_wrong_number_of_arguments_unchecked(t *testing.T) { {`fn().N(toJSON())`, "invalid number of arguments for toJSON (expected 1, got 0)"}, {`fn().N(date())`, "invalid number of arguments for date (expected at least 1, got 0)"}, {`fn().N(upper())`, "invalid number of arguments for upper (expected 1, got 0)"}, + {`fn().N(max())`, "invalid number of arguments for max (expected at least 1, got 0)"}, + {`fn().N(min())`, "invalid number of arguments for min (expected at least 1, got 0)"}, + {`fn().N(mean())`, "invalid number of arguments for mean (expected at least 1, got 0)"}, + {`fn().N(median())`, "invalid number of arguments for median (expected at least 1, got 0)"}, + {`fn().N(first())`, "invalid number of arguments for first (expected 1, got 0)"}, + {`fn().N(last())`, "invalid number of arguments for last (expected 1, got 0)"}, } for _, test := range tests { t.Run(test.input, func(t *testing.T) { diff --git a/compiler/compiler.go b/compiler/compiler.go index 32cf83c49..c250dec3f 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1218,6 +1218,7 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) { // call on an unknown value, as in `x.Method(join())`. Such a call fails at run // time with this error instead of indexing past the arguments or underflowing // the stack. Builtins with only a Validate function check their own arguments. +// Extra arguments are evaluated and ignored, as before. func builtinArgsError(f *builtin.Function, n int) error { if f.Fast != nil { if n == 0 {