Versions Compared

Key

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

...

...

...

...

...

...

...

Noncompliant Code Example (assert)

In this noncompliant code example, the standard assert() macro is suppressed in an attempt to pass it as a function pointer to the  execute_handler() function. Attempting to suppress the assert() macro is undefined behavior.

Code Block
bgColor#FFcccc
langc
#include <assert.h>
 
typedef void (*handler_type)(int);
 
void execute_handler(handler_type handler, int value) {
  handler(value);
}
 
void func(int e) {
  execute_handler(&(assert), e < 0);
} 

...

In this compliant solution, the assert() macro is wrapped in a helper function, removing the undefined behavior:

Code Block
bgColor#ccccff
langc
#include <assert.h>
 
typedef void (*handler_type)(int);
 
void execute_handler(handler_type handler, int value) {
  handler(value);
}
 
static void assert_handler(int value) {
  assert(value);
}
 
void func(int e) {
  execute_handler(&assert_handler, e < 0);
}

...

Legacy code is apt to include an incorrect declaration, such as the following in this noncompliant code example:

Code Block
bgColor#FFcccc
langc
extern int errno;

Compliant Solution (Declaring errno)

The This compliant solution demonstrates the correct way to declare errno is to include by including the header <errno.h>:

Code Block
bgColor#ccccff
langc
#include <errno.h>

...

Accessing objects or functions underlying these the specific macros results enumerated in this rule is undefined behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MSC38-C

Low

Unlikely

Medium

P2

L3

...

Bibliography

ISO/IEC 9899:2011Subclause 7.1.4, "Use of Library Functions"

...