Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Accessing With one exception accessing or modifying shared objects in signal handlers can lead to race conditions, opening up security holes.

...

The C99 standard dictates the use of volatile sig_atomic_t. The type of sig_atomic_t is implementation defined, although there are bounding constraints. To be fully compliant, you one can only assign values from 0 to 127, inclusive, to a sig_atomic_t.

...

Signal handlers should only unconditionally set and flaga flag of type volatile sig_atomic_t, and then return.

Code Block
bgColor#ccccff
#include <signal.h> 
#include <stdlib.h>
#include <string.h>
 
char *err_msg; 
volatile sig_atomic_t e_flag = 0;
 
void handler() { 
  e_flag = 1;
} 
 
int main() { 
  signal(SIGINT, handler); 

  err_msg = malloc(24);
  strcpy(err_msg, "No errors yet.");
 
  /* main code loop */
  if(e_flag)
    strcpy(err_msg, "SIGINT received.");


  return 0;
}

...