Skip to content

Conversation

@thevilledev
Copy link
Contributor

@thevilledev thevilledev commented Dec 3, 2025

Motivation

The builtin functions flatten, min, max, mean, and median recursively traverse nested arrays. If the environment provides a deeply nested structure or one containing a cycle (e.g., a slice containing itself), these functions would recurse indefinitely until the Go runtime panics due to stack overflow. This panic is unrecoverable and crashes the host application, presenting a DoS risk.

Changes

The builtin package now has a MaxDepth integer (defaults to 10k).

These recursive helper functions now accept a depth argument. This is incremented on recursive calls, and errors propagate up the stack if limit is exceeded. Initial function definitions call the helpers with an initial depth of 0 and handle returned errors.

Tests

Added various tests for the affected builtin functions using self-referencing slices. Also a test about customising the MaxDepth for users who rely on expr-lang as a library, and need more or less depth.

Add builtin.MaxDepth (default 10k) to prevent stack overflows when
processing deeply nested or cyclic structures in builtin functions.
The functions flatten, min, max, mean, and median now return a
"recursion depth exceeded" error instead of crashing the runtime.

Signed-off-by: Ville Vesilehto <[email protected]>
@antonmedv antonmedv merged commit 3dbda4b into expr-lang:master Dec 4, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants