Segmentation fault handling Segmentation fault handling c c

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!


I do not agree at all with the statement "Don't catch the SIGSEGV".

That's a pretty good pratice to deal with unexpected conditions. And that's much cleaner to cope with NULL pointers (as given by malloc failures) with signal mechanism associated to setjmp/longjmp, than to distribute error condition management all along your code.

Note however that if you use ''sigaction'' on SEGV, you must not forget to say SA_NODEFER in sa_flags - or find another way to deal with the fact SEGV will trigger your handler just once.

#include <setjmp.h>#include <signal.h>#include <stdio.h>#include <string.h>static void do_segv(){  int *segv;  segv = 0; /* malloc(a_huge_amount); */  *segv = 1;}sigjmp_buf point;static void handler(int sig, siginfo_t *dont_care, void *dont_care_either){   longjmp(point, 1);}int main(){  struct sigaction sa;  memset(&sa, 0, sizeof(sigaction));  sigemptyset(&sa.sa_mask);  sa.sa_flags     = SA_NODEFER;  sa.sa_sigaction = handler;  sigaction(SIGSEGV, &sa, NULL); /* ignore whether it works or not */   if (setjmp(point) == 0)   do_segv();  else    fprintf(stderr, "rather unexpected error\n");  return 0;}


If the SIGSEGV fires again, the obvious conclusion is that the call to MyfreeBuffers(); has not fixed the underlying problem (and if that function really does only free() some allocated memory, I'm not sure why you would think it would).

Roughly, a SIGSEGV fires when an attempt is made to access an inaccessible memory address. If you are not going to exit the application, you need to either make that memory address accessible, or change the execution path with longjmp().