Versions Compared


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


Code Block
#include <signal.h>

char *foo;

void int_handler() {

int main(void) {
  foo = (char *)malloc(sizeof("Hello World."));
  if (foo == NULL) {
    /* handle error condition */
  signal(SIGINT, int_handler);
  strcpy(foo, "Hello World.");
  /* ... */
  return 0;

This program has two potential problems. The first is that the free() function is not asynchronous-safe and its invocation from within a signal handler is a violation of this rule. If an interrupt signal is received during the free() call in main(), the heap may be corrupted.


The OpenBSD signal() man page also says:

Code Block
     A few other functions are signal race safe in OpenBSD but
     probably not on other systems:

           snprintf()    Safe.
           vsnprintf()   Safe.
           syslog_r()    Safe if the syslog_data struct is initialized
                         as a local variable.