Skip to main content

IAR Embedded Workbench for RX 5.20

CERT-ENV32-C

In this section:
Synopsis

All exit handlers must return normally

Enabled by default

Yes

Severity/Certainty

Medium/High

mediumhigh.png
Full description

A nested call to an exit function is undefined behavior. This behavior can occur when an exit function is invoked from an exit handler or when an exit function is called from within a signal handler. Exit handlers must terminate by returning. It is important and potentially safety-critical for all exit handlers to be allowed to perform their cleanup actions.

Coding standards
CERT ENV32-C

All atexit handlers must return normally

Code examples

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

#include <stdlib.h>

void exit1(void) {
  /* ... Cleanup code ... */
  return;
}

void exit2(void) {
  extern int some_condition;
  if (some_condition) {
    /* ... More cleanup code ... */
    exit(0);
  }
  return;
}

int main(void) {
  if (atexit(exit1) != 0) {
    /* Handle error */
  }
  if (atexit(exit2) != 0) {
    /* Handle error */
  }
  /* ... Program code ... */
  return 0;
}

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

#include <stdlib.h>

void exit1(void) {
  /* ... Cleanup code ... */
  return;
}

void exit2(void) {
  extern int some_condition;
  if (some_condition) {
    /* ... More cleanup code ... */
  }
  return;
}

int main(void) {
  if (atexit(exit1) != 0) {
    /* Handle error */
  }
  if (atexit(exit2) != 0) {
    /* Handle error */
  }
  /* ... Program code ... */
  return 0;
}