\[[ISO/IEC 9899-1999|AA. References#ISO/IEC 9899-1999]\] enumerates several instances where the behavior of accessing the object or function expanded to be a standard library macro definition is [undefined |BB. Definitions#undefined behavior]. |
The macros are assert, errno, math_errhandling, setjmp, va_start, va_arg, va_copy, and va_end.
These cases are recorded in the list of undefined behavior, items 104, 108, 116, 118 and 130.
Programmers should never attempt to access anything underlying any of these macros.
In this example, a programmer attempts to access their own verification functionality by suppressing the assert macro and instead sending control to an assert function.
#include <assert.h>
// suppose the following is the definition of the assert()
// macro in <assert.h> and there is no function with the
// same name:
// #define assert(expr) \
// ((expr) ? (void)0 \
// : (void)fprintf(stderr, "Assertion failed: %s:%d (%s): %s\n", \
// __FILE__, __LINE__, __func__, #expr))
//
void f(int i) {
(assert)(0 < i); // assert() macro suppressed, calling function assert()
}
|
Having this function and attempting to access it does not produce defined behavior.
The programmer should place nonstandard verification in a function that does not conflict with the standard library macro assert.
#include <assert.h>
void f(int e) {
customAssert(e); // function will check desired assertions
}
|
Legacy code is apt to include an incorrect declaration such as the following.
extern int errno; |
The correct way to declare errno is to include the header <errno.h>.
#include <errno.h> |
Implementations conforming to C99 are required to declare errno in <errno.h>, although some historic implementations failed to do so.
Accessing objects or function underlying these macros does not produce defined behavior, which may lead to incorrect or unexpected program behavior.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
MSC38-C |
low |
unlikely |
medium |
P2 |
L3 |
\[[ISO/IEC 9899:1999|AA. References#ISO/IEC 9899-1999]\], all sections indicated by the [undefined behavior items |CC. Undefined Behavior] noted above. |
koders.com, <assert.h> source.