...
In this noncompliant code example [GCC Bugs], the programmer uses preprocessor directives to specify platform-specific arguments to memcpy(). However, if memcpy() is implemented using a macro, the code results in in undefined behavior 92.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <string.h>
void func(const char *src) {
/* Validate the source string; calculate size */
char *dest;
/* malloc() destination string */
memcpy(dest, src,
#ifdef PLATFORM1
12
#else
24
#endif
);
/* ... */
}
|
...
Including preprocessor directives in macro arguments is is undefined behavior 92.
Rule | Severity | Likelihood | Detectable | Remediation CostRepairable | Priority | Level |
|---|---|---|---|---|---|---|
PRE32-C | Low | Unlikely | Yes | MediumYes | P2P3 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Astrée |
| macro-argument-hash | Fully checked | ||||||
| Axivion Bauhaus Suite |
| CertC-PRE32 | Fully implemented | ||||||
| CodeSonar |
| LANG.PREPROC.MACROARG | Preprocessing directives in macro argument | ||||||
| CC2.PRE32 | Fully implemented | |||||||
| Helix QAC |
| C0853 C++1072 | Fully implemented | ||||||
| Klocwork |
| MISRA.EXPANSION.DIRECTIVE | Fully implemented | ||||||
| LDRA tool suite |
| 341 S | Fully implemented | ||||||
| Parasoft C/C++test |
| CERT_C-PRE32-a | Arguments to a function-like macro shall not contain tokens that look like preprocessing directives | ||||||
| PC-lint Plus |
| 436, 9501 | Fully supported | ||||||
| CERT C: Rule PRE32-C | Checks for preprocessor directive in macro argument (rule fully covered) | |||||||
| RuleChecker |
| macro-argument-hash | Fully checked |
...