Segmentation fault handling

The default action for things like SIGSEGV is to terminate your process but as you’ve installed a handler for it, it’ll call your handler overriding the default behavior. But the problem is segfaulting instruction may be retried after your handler finishes and if you haven’t taken measures to fix the first seg fault, the retried instruction will again fault and it goes on and on.

So first spot the instruction that resulted in SIGSEGV and try to fix it (you can call something like backtrace() in the handler and see for yourself what went wrong)

Also, the POSIX standard says that,

The behavior of a process is undefined after it returns normally from
a signal-catching function for a [XSI] SIGBUS, SIGFPE, SIGILL, or
SIGSEGV signal that was not generated by kill(), [RTS] sigqueue(),
or raise().

So, the ideal thing to do is to fix your segfault in the first place. Handler for segfault is not meant to bypass the underlying error condition

So the best suggestion would be- Don’t catch the SIGSEGV. Let it dump core. Analyze the core. Fix the invalid memory reference and there you go!

Leave a Comment