Versions Compared

Key

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

...

Code Block
bgColor#ffcccc
langc
#include <signal.h>
#include <stddef.h>
#include <stdlib.h>

volatile sig_atomic_t denom;

void sighandle(int s){
  /* Fix the offending volatile. */
  if (denom == 0) {
    denom = 1;
  }
}

int main(int argc, char *argv[]){
  int result = 0;
    
  if (argc < 2) {
    return 0;
  }
  denom = (sig_atomic_t)strtol(argv[1], NULL, 10);
    
  signal(SIGFPE,(*sighandle));

  result = 100 / (int)denom;
  return 0;
}

The noncompliant code example will loop infinitely on input 0 when compiled with GCC 4.3 or GCC 3.4. It illustrates that even when a SIGFPE handler attempts to fix the error condition while obeying all other rules of signal handling, the program still does not behave as expected.

...

Code Block
bgColor#ccccff
langc
#include <signal.h>
#include <stddef.h>
#include <stdlib.h>

volatile sig_atomic_t denom;

void sighandle(int s){
  /* Recovery is impossible. */
  abort();
}

int main(int argc, char *argv[]){
  int result = 0;
    
  if (argc < 2) {
    return 0;
  }
  denom = (sig_atomic_t)strtol(argv[1], NULL, 10);
    
  signal(SIGFPE,(*sighandle));

  result = 100 / (int)denom;
  return 0;
}

Implementation Details

...