The C Standard [ISO/IEC 9899-1999:2011] enumerates several instances where the behavior of accessing an object or function expanded to be a standard library macro definition is undefined.
...
These cases are recorded in the list of undefined Annex J, section J.2, Undefined behavior, items 104 110, 108 114, 116 122, 118 124, and 130 138 [ISO/IEC 9899:2011].
Programmers should never attempt to access anything underlying any of these macros.
...
In this example, a programmer attempts to access their access his own verification functionality by suppressing the assert
macro and instead sending control to a user-defined assert()
function.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <myassert.h>
#include <assert.h>
void fullAssert(int e) {
assert(0 < e); // invoke standard library assert()
(assert)(0 < e); // assert() macro suppressed, calling function assert()
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <myassert.h>
#include <assert.h>
void fullAssert(int e) {
assert(0 < e); // standard library assert()
myassert(e); // well defined custom assertion function
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
extern int errno;
|
Compliant Solution (Redefining errno
)
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <errno.h>
|
Implementations conforming to C99 are C-conforming implementations are required to declare errno
in <errno.h>
, although some historic implementations failed to do so.
Risk Assessment
Accessing objects or function functions 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 |
Related Guidelines
ISO/IEC 9899:1999: all sections indicated by the undefined behavior items noted above
...
2011 Section 7.2, "Diagnostics <assert.h>
," Section 7.5, "Errors <errno.h>
," Section 7.12, "Mathematics <math.h>
," Section 7.13 "Nonlocal jumps <setjmp.h>
," and Section 7.16.1, "Variable argument list access macros"
...