Is it true that the segmentation violation exception can only be triggered once?

43 Views Asked by At

I tested the code below (

  • first, the signal handler is installed,

  • then the return position environment is saved,

  • finally the exception is triggered and the control flow jump back to the start of the exception by the signal handler

)

The result is: only one SEGV signal is catched.

But, when I uncomment the assert line, the ABRT signal is catched infinitely.

#include <stdio.h>
#include <assert.h>
#include <signal.h>
#include <setjmp.h>
jmp_buf g_position_env;
void handle_abort(int sig) {
    printf("catch abort\n");
    longjmp(g_position_env, 1);
}
void handle_segment_violation(int sig) {
    printf("catch segv\n");
    longjmp(g_position_env, 2);
}
int main() {
    unsigned int i;
    int ret;
    signal(SIGABRT, handle_abort);
    signal(SIGSEGV, handle_segment_violation);
    i = 0;
    ret = setjmp(g_position_env);
    if (ret == 1) {
        goto loop_abrt;
    } else if (ret == 2) {
        i++;
        goto loop_segv;
    }    
loop_abrt:
// uncomment code below, it will enter a loop
    //assert(1 == 3);
loop_segv: 
   printf("i = %d\n", i);
   printf("%d\n", *(int *)i);
  
    printf("-----------end\n");
    return 0;
}

0

There are 0 best solutions below