...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#define END_OF_FILE -1 /* ... */ if (getchar() EOFEND_OF_FILE) { /* ... */ } |
In this example, the programmer has mistakenly omitted the comparison operator from the conditional statement, which should be getchar() != END_OF_FILE. (See void MSC02-C. Avoid errors of omission.) After macro expansion, the conditional expression is incorrectly evaluated as a binary operation: getchar()-1. This statement is syntactically correct, even though it is certainly not what the programmer intended. Note that this example also violates DCL00-C. Const-qualify immutable objects.
...
Failing to parenthesize macro replacement lists can cause unexpected results.
Recommendation | Severity | Likelihood | Detectable |
|---|
Repairable | Priority | Level |
|---|---|---|
PRE02-C | Medium | Probable |
Yes | Yes | P12 | L1 |
Automated Detection
| Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Axivion Bauhaus Suite |
| CertC-PRE02 | |||||||
| CodeSonar |
| LANG.PREPROC.MACROEND LANG.PREPROC.MACROSTART | Macro Does Not End With ) or } Macro Does Not Start With ( or { | ||||||
| CC2.PRE02 | Fully implemented | |||||||
| Helix QAC |
| C3409 | |||||||
| Klocwork |
| MISRA.DEFINE.BADEXP | |||||||
| LDRA tool suite |
| 77 S | Fully implemented | ||||||
| Parasoft C/C++test |
| CERT_C-PRE02-a | Enclose the entire definition of a function-like macro in parentheses | |||||||
| PC-lint Plus |
| 773, 973 | Fully supported |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
| SEI CERT C++ Coding Standard | VOID PRE02-CPP. Macro replacement lists should be parenthesized |
| ISO/IEC TR 24772:2013 | Operator Precedence/Order of Evaluation [JCW] Pre-processor Directives [NMP] |
Bibliography
| [Plum 1985] | Rule 1-1 |
| [Summit 2005] | Question 10.1 |
...
...