Skip to content

Commit 2fb1f53

Browse files
committed
add safety limit on preallocation
1 parent 64d5d1c commit 2fb1f53

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

vm/vm.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"github.com/expr-lang/expr/vm/runtime"
1818
)
1919

20+
const maxFnArgsBuf = 256
21+
2022
func Run(program *Program, env any) (any, error) {
2123
if program == nil {
2224
return nil, fmt.Errorf("program is nil")
@@ -615,6 +617,10 @@ func (vm *VM) getArgsForFunc(argsBuf []any, program *Program, needed int) (args
615617
// Step 1: fix estimations and preallocate
616618
if argsBuf == nil {
617619
estimatedFnArgsCount := estimateFnArgsCount(program)
620+
if estimatedFnArgsCount > maxFnArgsBuf {
621+
// put a practical limit to avoid excessive preallocation
622+
estimatedFnArgsCount = maxFnArgsBuf
623+
}
618624
if estimatedFnArgsCount < needed {
619625
// in the case that the first call is for example OpCallN with a large
620626
// number of arguments, then make sure we will be able to serve them at

0 commit comments

Comments
 (0)