Repository navigation
expr: prevent stack overflow on deeply nested parentheses - #13404
koopatroopa787 wants to merge 1 commit into
Conversation
Deeply nested parentheses like (((...))) recurse through
parse_simple_expression → parse_expression → parse_precedence (×6) for
each level — ~8 frames per paren. At ~105 levels in debug builds the
default thread stack is exhausted, producing a SIGSEGV.
Add a depth counter to the Parser struct and return a new
ExprError::RecursionLimit error ("expression too deeply nested", exit 2)
when the paren nesting exceeds MAX_RECURSION_DEPTH (86). The limit is
chosen to cap the recursive stack at ~688 frames, providing comfortable
headroom below the observed debug-build overflow threshold.
Fixes uutils#13146
Merging this PR will degrade performance by 3.29%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Simulation | du_max_depth_balanced_tree[(6, 4, 10)] |
25.5 ms | 26.5 ms | -3.48% |
| ❌ | Simulation | du_all_wide_tree[(5000, 500)] |
16.2 ms | 16.8 ms | -3.1% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing koopatroopa787:fix-expr-stack-overflow-deep-parens (639b0d6) with main (76d7a53)2
Footnotes
-
46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
-
No successful run was found on
main(67cc7a1) during the generation of this report, so 76d7a53 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩
|
codspeed was actually saying invalid benchmarks, @sylvestre can we have a workaround a workflow that use hyperfine or similar, codspeed is reporting highly invalid results |
|
#13333 might be a better approach? |
|
GNU testsuite comparison: |
The discrepancy comes from the different runtime environments (AMD EPYC 7763 64-Core Processor vs. Intel(R) Xeon(R) 6973P-C). Since GitHub Actions doesn’t provide consistent hardware across runners, this isn’t something we can realistically work around. In any case, since there were no changes to |
|
Will close since #13333 was merged |
Summary
Deeply nested parentheses like
(((...)))cause a stack overflow inexprbecause the recursive descent parser creates ~8 stack frames per paren level (oneparse_simple_expression, oneparse_expression, and sixparse_precedencecalls). At ~105 levels the default thread stack is exhausted, producing a process abort.Fix
Added a
depth: usizecounter to theParserstruct. Whenparse_simple_expressionencounters(andself.depth >= MAX_RECURSION_DEPTH, it returnsExprError::RecursionLimit("expression too deeply nested", exit code 2) instead of recursing further.MAX_RECURSION_DEPTH = 86caps the recursive stack at ~688 frames, providing comfortable headroom below the observed ~840-frame overflow threshold in debug builds.Test
A new regression test
test_deeply_nested_parens_do_not_crashpasses 5000 levels of nesting toexprand verifies it exits with code 2 and prints the error message, rather than crashing.Fixes #13146