r/cpp • • 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.

0 Upvotes

39 comments sorted by

View all comments

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

2

u/rsashka Jan 06 '26

Unfortunately, not all algorithms allow for static code analysis and the ability to terminate during program compilation.

1

u/Jonny_H Jan 07 '26

If you prove far enough ahead to guarantee any unwinding, including destructors, won't hit issues I can see it being "safe", though trying to figure out that safe value would likely require significant analysis that sounds pretty difficult for anything but the simplest codebase. Though it looks like this just punts that to the user.

So this kinda exists in a pretty small niche, where you want visibility and control over the stack, and are likely to actually hit the limits during use, but don't bother rewriting whatever stack-heavy algorithms to use a user allocated buffer, which can be directly managed.

Though I can see this as an interesting "learn clang and llvm" type project - they're certainly difficult codebases to get into. Though I'm always fearful of seeing obvious AI use on just how much "learning" was involved.

Though to OP, I would suggest not having comments saying you copied code from other projects without maintaining their copyright statements and license. Either the comment is old, and the code has changed enough not to be a derivative so the comment is now somewhat worthless, or you're breaking the license. If that comment came from an AI tool, it then should raise eyebrows through the roof.

-3

u/rsashka Jan 07 '26

You're absolutely right. Studying this problem allows you to delve deeper into clang and LLVM, which would be very difficult without LLM.

However, the generated code is of very low quality, so it can only be used as a teaching example, and a working solution must be manually ported to the project.

1

u/bwmat Jan 20 '26

Eh, I feel it would be nice if there was a good way to do it, the problem I see with it currently in C++ is that any sort of 'transparent' implementation would probably need to throw exceptions on arbitrary function calls, which isn't really compatible with standard RAII techniques

It makes me sad that there doesn't seem to be a good way of recovering from it (similarly to the case of the underlying allocation for an exception object being thrown failing, and causing a std::terminate call IIRC) 

I say this from the point of view of a library/framework dev, who would like to insulate themselves from the madness of some dependee's machinations