Repository navigation
Fix parser stack overflow and MemoryBudget bypasses - #1003
Merged
Merged
Conversation
Deeply nested input such as ~800KB of '(' or '[' overflowed the Go stack
in the recursive-descent parser. A stack overflow is fatal and cannot be
recovered, and MaxNodes does not help because parentheses create no nodes.
Track recursion depth in parseExpression and fail with a compile error
once it reaches parser.MaxNesting (10000).
Several operations allocated without being charged against MemoryBudget, so a small expression could allocate gigabytes: - string concatenation (e.g. reduce(1..34, #acc + #acc, "a")) - repeat, replace, join, split, splitAfter - toJSON, toBase64, and string results of single-argument builtins String results are charged for the bytes they add beyond their largest input, so concatenating with a large env string still works. Composite inputs are charged the full output, to prevent doubling through aliasing. Safe builtins now receive the remaining budget, so repeat, replace and join can refuse an oversized result before allocating it. Breaking: builtin.Function.Safe and vm.SafeFunction take the remaining budget as their first argument. Safe builtins are compiled to the new OpCallBuiltinSafe opcode, which avoids pushing the function as a constant. join now returns an error for non-string elements.
This was referenced Nov 21, 2026
estimateFnArgsCount walked the whole bytecode on the first function call of every run. Compute it once in NewProgram and reuse it. Programs built without NewProgram still compute it on the fly. This removes a 10% slowdown in BenchmarkVM/function_calls introduced by the exact OpCallSafe/OpCallBuiltinSafe estimate, and makes it 4% faster than before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two denial-of-service fixes, one per commit.
Parser: limit nesting depth
Deeply nested input (e.g. ~800KB of
(or[) overflowed the Go stack in the parser. A stack overflow is fatal and can't be recovered, andMaxNodesdoesn't help because parentheses create no nodes. The parser now returns a compile error once nesting reachesparser.MaxNesting(10,000).VM: enforce MemoryBudget for string and builtin allocations
String
+,repeat,replace,join,split,splitAfter,toJSON,toBase64and string results of single-argument builtins were not charged againstMemoryBudget, so a short expression could allocate gigabytes."prefix" + bigEnvStringstill works under the default budget.[#acc, #acc]) can't double a string for free.repeat,replaceandjoincheck the remaining budget before allocating.Breaking changes
builtin.Function.Safeandvm.SafeFunctiontake the remaining budget as their first argument.OpCallBuiltinSafefor Safe builtins.joinreturns an error for non-string elements (the same fix as return error on non-string element in join #969).joinortoJSONof an array or map producing more than ~1 MB, and building long strings one character at a time inreduce, now hit the default budget.Performance
Compared with master in alternating runs, 10 rounds each:
+is 3.4% faster andrepeatis 4% faster.split,join,replaceandtoBase64are 3–4.5% slower (about 3 ns per call).Not covered
A loop that copies a large string many times (e.g.
map(1..N, s + "x")) is still not fully bounded. Closing that requires charging every byte, which would break users who work with large data on the default budget.Supersedes #938.