[clang] [clang] Prevent stack overflow on deeply nested template arguments (PR #224393)

Daniel Petrovic via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 21 13:52:24 PDT 2026


daniel-petrovic wrote:

> I don't have strong opinions about the approach taken here, but it seems like we're punishing the simple case of a single level of templates in order to better diagnose more pathological cases.

Just a small brain-storming:

As I understand, the current StackHandler.runWithSufficientStackSpace() does not  differentiate the recursion level  at the moment : the same checks are done for 1 as well as for 1000 levels of recursion.
What if the StackHandler would track the recursion level and do something like:
```
if (recursion_level < threshold_level) [[ likely ]] {
  F(); // just recurse normaly
} else {
  // the current logic
  // if (isStackNearlyExhausted()) [[ unlikely ]]  {...}
}
```
What we want to achieve with this:
 
- up to some small number of recursions just recurse normally ( by assuming we will never exceed stack limit on this recursion level)
- for deeper recursions start doing more checks.

Some rough idea: https://godbolt.org/z/ovvMY46zT

Of course, measurements would have to confirm  benefits of such an approach, if any at all.

 But for the case it's positive, it would cover all scenarios: cheap for small recursion debts and robust for fuzzing.

Just an idea.

https://github.com/llvm/llvm-project/pull/224393


More information about the cfe-commits mailing list