Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,13 @@ func TestBuiltin_errors(t *testing.T) {
}
}

// The get() builtin must return an error when called with
// insufficient arguments at runtime, even if compile-time checks
// are bypassed (regression test for OSS-Fuzz #479270603).
func TestBuiltin_get_runtime_args_check(t *testing.T) {
func TestBuiltin_env_not_callable(t *testing.T) {
code := `$env(''matches'i'?t:get().UTC())`
env := map[string]any{"t": 1}

program, err := expr.Compile(code, expr.Env(env))
require.NoError(t, err)

_, err = expr.Run(program, env)
_, err := expr.Compile(code, expr.Env(env))
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid number of arguments")
assert.Contains(t, err.Error(), "is not callable")
}

func TestBuiltin_types(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@ func (v *Checker) callNode(node *ast.CallNode) Nature {
return *node.Nature()
}

// $env is not callable.
if id, ok := node.Callee.(*ast.IdentifierNode); ok && id.Value == "$env" {
return v.error(node, "%s is not callable", v.config.Env.String())
}

nt := v.visit(node.Callee)
if nt.IsUnknown(&v.config.NtCache) {
return Nature{}
Expand Down
24 changes: 24 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,30 @@ invalid operation: > (mismatched types string and int) (1:30)
invalid operation: + (mismatched types int and bool) (1:6)
| 1; 2 + true; 3
| .....^
`,
},
{
`$env()`,
`
mock.Env is not callable (1:1)
| $env()
| ^
`,
},
{
`$env(1)`,
`
mock.Env is not callable (1:1)
| $env(1)
| ^
`,
},
{
`$env(abs())`,
`
mock.Env is not callable (1:1)
| $env(abs())
| ^
`,
},
}
Expand Down
38 changes: 31 additions & 7 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,20 +1512,44 @@ func TestVM_StackUnderflow(t *testing.T) {
}
}

func TestVM_OpCall_InvalidNumberOfArguments(t *testing.T) {
// This test ensures that calling a function with wrong number of arguments
// produces a clear error message instead of a panic.
// Regression test for clusterfuzz issue with expression:
// $env(''matches' '? :now().UTC(g).d)//

func TestVM_EnvNotCallable(t *testing.T) {
// $env is the environment, not a function.
env := map[string]any{
"ok": true,
}

code := `$env('' matches ' '? : now().UTC(g))`
program, err := expr.Compile(code, expr.Env(env))
_, err := expr.Compile(code, expr.Env(env))
require.Error(t, err)
require.Contains(t, err.Error(), "is not callable")
}

func TestVM_OpCall_InvalidNumberOfArguments(t *testing.T) {
// Test that the VM validates argument count at runtime.
// Compile without Env() so compiler generates OpCall without type info.
program, err := expr.Compile(`fn(1, 2)`)
require.NoError(t, err)

// Run with a function that has different arity
env := map[string]any{
"fn": func(a int) int { return a },
}

_, err = expr.Run(program, env)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid number of arguments")
}

func TestVM_OpCall_InvalidNumberOfArguments_Variadic(t *testing.T) {
// Test variadic function with too few arguments.
program, err := expr.Compile(`fn()`)
require.NoError(t, err)

// Run with a variadic function that requires at least 1 argument
env := map[string]any{
"fn": func(first int, rest ...int) int { return first },
}

_, err = expr.Run(program, env)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid number of arguments")
Expand Down
Loading