Versions Compared

Key

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

...

This noncompliant code example defines a function that is called before the program exits to clean up.:

Code Block
bgColor#ffcccc
langc
void cleanup(void) {
  /* Delete temporary files, restore consistent state, etc. */
}

int main(void) {
  if (atexit(cleanup) != 0) {
    /* Handle error */
  }

  /* ... */

  assert(/* something bad didn't happen */);

  /* ... */
}

...

In this compliant solution, the call to assert() is replaced with an if statement that calls exit() to ensure that the proper termination routines are run.:

Code Block
bgColor#ccccff
langc
void cleanup(void) {
  /* Delete temporary files, restore consistent state, etc */
}

int main(void) {
  if (atexit(cleanup) != 0) {
    /* Handle error */
  }

  /* ... */

  if (/* something bad happened */) {
    exit(EXIT_FAILURE);
  }

  /* ... */
}

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

Can detect some violations of this rule. However, it can only detect violations involving abort() because assert() is implemented as a macro.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...