Versions Compared

Key

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

Macro replacement lists should be parenthesized to protect any lower-precedence operators from the surrounding expression. See also guidelines PRE00-C. Prefer inline or static functions to function-like macros and PRE01-C. Use parentheses within macros around parameter names.

...

This compliant solution violates guideline PRE00-C. Prefer inline or static functions to function-like macros. Consequently, this solution would be better implemented as an inline function.

...

In this example, the programmer has mistakenly omitted the comparison operator (see MSC02-C. Avoid errors of omission) from the conditional statement, which should be getchar() != EOF. (See guideline MSC02-C. Avoid errors of omission.) After macro expansion, the conditional expression is incorrectly evaluated as a binary operation: getchar()-1. This is syntactically correct, even though it is certainly not what the programmer intended. Note that this example also violates guideline DCL00-C. Const-qualify immutable objects.

...

Once this modification is made, the noncompliant code example no longer compiles because the macro expansion results in the conditional expression getchar() (-1), which is no longer syntactically valid. Note that there must be a space after EOF because, otherwise, it becomes a function-like macro (and one that is incorrectly formed , because -1 cannot be a formal parameter).

...

In this compliant solution, the macro definition is replaced with an enumeration constant in compliance with guideline DCL00-C. Const-qualify immutable objects. In addition, since EOF is a reserved macro defined in the <stdio.h> header, the compliant solution must also use a different indentifier in order to comply with guideline DCL37-C. Do not use identifiers that are reserved for the implementation.

...

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

PRE02-C

medium

probable

low

P12

L1

Automated Detection

...

Tool

Version

Checker

Description

Section

LDRA tool suite

...

Include Page
c:LDRA_V
c:LDRA_V

 

 

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

Related Guidelines

This rule appears in the C++ Secure Coding Standard as : PRE02-CPP. Macro replacement lists should be parenthesized.

Bibliography

Wiki Markup
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 6.10, "Preprocessing directives," and Section 5.1.1, "Translation environment"
\[[ISO/IEC PDTR 24772|AA. Bibliography#ISO/IEC PDTR 24772]\] "JCW Operator precedence/Order of Evaluation", "NMP Pre-processor Directions"
\[[Plum 851985|AA. Bibliography#Plum 85]\] Rule 1-1
\[[Summit 052005|AA. Bibliography#Summit 05]\] Question 10.1

...