The customized abnormal signal handler (e.g. SIGSEGV handler) will function abnormally if we call exit() inside the handler: the program will exit “as normal” however it did had some severe memory issue.
See code snippet below, try to run with / without “X” argument.
/* exit example */ #include <stdio.h> /* printf, fopen */ #include <stdlib.h> /* exit, EXIT_FAILURE */ #include <signal.h> /* signal */ #include <string.h> bool disable_exit = false; void InitiateAbnormalShutdown() { printf("ABNORMAL SHUTDOWN!!!\n"); if (!disable_exit) exit(EXIT_FAILURE); } void AbnormalSignalHandler(int signo) { signal(signo, SIG_DFL); InitiateAbnormalShutdown(); raise(signo); } bool give_me_an_error() { int* test = new int; *test = 500; delete test; test = NULL; *test = 900; if (*test > 500) { return false; } return true; } int main(int argc, char *argv[]) { if (argc > 1) { printf("arg == %s\n", argv[1]); if (strcmp(argv[1], "X") == 0) { printf("disable exit on abnormal handler...\n"); disable_exit = true; } } signal(SIGSEGV, AbnormalSignalHandler); give_me_an_error(); return 0; }
Output:
bowei.he:/sandbox/bowei.he/temp$ ./sigf ABNORMAL SHUTDOWN!!! bowei.he:/sandbox/bowei.he/temp$ ./sigf X arg == X disable exit on abnormal handler... ABNORMAL SHUTDOWN!!! Segmentation fault (core dumped)