Skip to main content

IAR Embedded Workbench for Arm 9.70.x

CERT-SIG35-C

In this section:
Synopsis

Do not return from a computational exception signal handler.

Enabled by default

Yes

Severity/Certainty

Low/Low

lowlow.png
Full description

If a signal handler returns when it has been entered as a result of a computational exception (that is, with the value of its argument of SIGFPE, SIGILL, SIGSEGV, or any other implementation-defined value corresponding to such an exception) returns, then the behavior is undefined.

Coding standards
CERT SIG35-C

Do not return from SIGSEGV, SIGILL, or SIGFPE signal handlers

Code examples

The following code example fails the check and will give a warning:

#include <errno.h>
#include <limits.h>
#include <signal.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[]) {
  if (argc < 2) {
    return 0;
  }

  char *end = NULL;
  long temp = strtol(argv[1], &end, 10);

  if (end == argv[1] || 0 != *end ||
      ((LONG_MIN == temp || LONG_MAX == temp) && errno == ERANGE)) {
    /* Handle error */
  }

  denom = (sig_atomic_t)temp;
  signal(SIGFPE, sighandle);

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

The following code example passes the check and will not give a warning about this issue:

#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  if (argc < 2) {
    return 0;
  }

  char *end = NULL;
  long denom = strtol(argv[1], &end, 10);

  if (end == argv[1] || 0 != *end ||
      ((LONG_MIN == denom || LONG_MAX == denom) && errno == ERANGE)) {
    /* Handle error */
  }

  long result = 100 / denom;
  return 0;
}