@@ -1512,20 +1512,44 @@ func TestVM_StackUnderflow(t *testing.T) {
15121512 }
15131513}
15141514
1515- func TestVM_OpCall_InvalidNumberOfArguments (t * testing.T ) {
1516- // This test ensures that calling a function with wrong number of arguments
1517- // produces a clear error message instead of a panic.
1518- // Regression test for clusterfuzz issue with expression:
1519- // $env(''matches' '? :now().UTC(g).d)//
1520-
1515+ func TestVM_EnvNotCallable (t * testing.T ) {
1516+ // $env is the environment, not a function.
15211517 env := map [string ]any {
15221518 "ok" : true ,
15231519 }
15241520
15251521 code := `$env('' matches ' '? : now().UTC(g))`
1526- program , err := expr .Compile (code , expr .Env (env ))
1522+ _ , err := expr .Compile (code , expr .Env (env ))
1523+ require .Error (t , err )
1524+ require .Contains (t , err .Error (), "is not callable" )
1525+ }
1526+
1527+ func TestVM_OpCall_InvalidNumberOfArguments (t * testing.T ) {
1528+ // Test that the VM validates argument count at runtime.
1529+ // Compile without Env() so compiler generates OpCall without type info.
1530+ program , err := expr .Compile (`fn(1, 2)` )
15271531 require .NoError (t , err )
15281532
1533+ // Run with a function that has different arity
1534+ env := map [string ]any {
1535+ "fn" : func (a int ) int { return a },
1536+ }
1537+
1538+ _ , err = expr .Run (program , env )
1539+ require .Error (t , err )
1540+ require .Contains (t , err .Error (), "invalid number of arguments" )
1541+ }
1542+
1543+ func TestVM_OpCall_InvalidNumberOfArguments_Variadic (t * testing.T ) {
1544+ // Test variadic function with too few arguments.
1545+ program , err := expr .Compile (`fn()` )
1546+ require .NoError (t , err )
1547+
1548+ // Run with a variadic function that requires at least 1 argument
1549+ env := map [string ]any {
1550+ "fn" : func (first int , rest ... int ) int { return first },
1551+ }
1552+
15291553 _ , err = expr .Run (program , env )
15301554 require .Error (t , err )
15311555 require .Contains (t , err .Error (), "invalid number of arguments" )
0 commit comments