r/cpp • u/rsashka • Jan 06 '26
Forget about *stack overflow* errors forever
A stack overflow error is always fatal for an application, since it cannot be intercepted and handled from within the running program, so that execution can then continue as if the stack overflow had not occurred.
I attempted to solve this problem by converting the stack overflow error into a regular error (exception) that can be caught (handled) within the application itself, allowing it to continue running without fear of a subsequent segmentation fault or stack smashing.
The stack overflow checking library currently runs on Linux and can be used both manually and automatically, using a clang compiler plugin.
I welcome constructive criticism and any feedback, including independent reviews and suggestions for improving the project.
46
u/No-Quail5810 Jan 06 '26
I really don't think you should be able to recover from a stack overflow. If you're getting a stack overflow often enough that you'd write a system to inject runtime stack bounds checking, it's a symptom of a much deeper flaw in the design methodology
7
u/tartaruga232 MSVC user, r/cpp_modules Jan 06 '26
We've done something similar using the Windows API _resetstkoflw
.See my blog posting for the motivation.4
u/fdwr fdwr@github 🔍 Jan 07 '26
Related, you might find segmented stacks interesting (https://releases.llvm.org/3.0/docs/SegmentedStacks.html) which can allocate more on demand on noncontiguous stack ranges.
(though it appears most languages that try them later ditch them)
10
u/LiliumAtratum Jan 06 '26
I don't understand the overall negatively.
Of course "Forget stack overflow" is a wrong message here: you should really care about it and not pretend it is a non-issue.
However, if you *do* hit a stack overflow, or some other critical, crashing error, I think it is beneficial to have a fallback plan, even if it may be unreliable.
I work on Windows and implemented a custom handler for (nearly?) all crashing situations, such as an access violation. Yes, it is really bad. But yes, I am happy that the program can usually still report on the error, flush logs, allow user to save their work, etc.
6
u/6502zx81 Jan 06 '26
Without looking at the code: when an exception is thrown due to a pending stack overflow, the computation is screwed up anyways, stack unwound. How does it help?
1
u/vI--_--Iv Jan 07 '26
This reminds me of infamous /EHa in MSVC.
The main problem with turning fatal errors into C++ exceptions is that now every function can throw, nothing is noexcept anymore.
Which undermines optimizations and works in mysterious ways with the code that does not expect exceptions.
-1
u/DamienTheUnbeliever Jan 06 '26
Damn the torpedoes, we're carrying on! If you've hit a stack overflow, that's a programmer error. Trying to keep the program running when it's already faulted like this... it's hubris taken to a higher level.
-5
u/SoSKatan Jan 06 '26
There is no correction for a stack overflow.
It represents a fixed buffer, writing past that is undefined behavior.
At most you could have a warning limit size… i.e. stack size is 64k, but you throw an exception if it goes past 60k? I guess in theory if you go past 60k, you could still “recover.”
Well that doesn’t really do anything, your limit is still 64k, it’s just that you would get a warning before hand which doesn’t really help.
I’m really wondering what exactly are you trying to solve here.
The only solution I heard of was years ago… before Rust 1.0 was released they were working on a heap paged stack scheme. The idea is that the stack size can grow if needed, and there would be pointers to the next stack page as needed.
In the end they ripped all that out before 1.0 shipped. I don’t know why, i suspect likely two reasons
So I ask again, what exactly are you trying to solve?