Fix Uncontrolled Recursion in simplifyAddBracesToCommand #8264
Fix Uncontrolled Recursion in simplifyAddBracesToCommand #8264emptyiscolor wants to merge 2 commits intodanmar:mainfrom
simplifyAddBracesToCommand #8264Conversation
|
Thanks for your contribution. If we go with the depth limit, we should probably throw an error when the limit is exceeded. Also please add a test. |
We have lots of depth bailouts in the code which report nothing so this should be fine. I have a commit which flags all of those and I would like to introduce debug messages when they are hit - and in a perfect world also store all the limits in a single place so they could be tuned (at least being documented instead of hidden away in some default parameters). |
|
Not having a testcase is not good though. And I am wondering if the depth should be implemented the other way around. The limit is set in the default parameter and it is counted down - as we do in other cases. That would prevent unbounded calls being introduced in future changes. |
|
|
Thanks for the feedback. The updated code switched to the count-down depth pattern used elsewhere in the codebase: |



Related case: https://sourceforge.net/p/cppcheck/discussion/development/thread/eea4fd963a/
Applied Fix — Recursion Depth Limit
Added an
int depthparameter (default0) to both functions. Every recursive call passesdepth + 1, and at the top ofsimplifyAddBracesToCommandwe bail out whendepth > 500:Test
Both PoC vectors (100K
else ifchain and 50K nestedif) no longer cause a stack overflow by running the PY code from the discussion. Normal code continues to be analyzed correctly.Alternative Fix — Iterative Approach
The
else ifchain recursion (Path 1) can be eliminated by converting the tail-recursive call into a loop insidesimplifyAddBracesToCommand.This eliminates Path 1 (else-if chain) with zero stack growth regardless of chain length. Path 2 (mutual recursion with
simplifyAddBracesPair) would still need a depth limit or its own iterative conversion using an explicit stack/worklist.A full iterative conversion of both paths is a larger refactor but would make the code completely immune to stack overflow from any input shape.