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 recommendations 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 recommendation PRE00-C. Prefer inline or static functions to function-like macros. Consequently, this solution would be better implemented as an inline function.

...

In this noncompliant code example, EOF is defined as -1. The macro replacement list consists of a unary negation operator - , followed by an integer literal 1.

...

In this example, the programmer has mistakenly omitted the comparison operator from the conditional statement, which should be getchar() != EOF. (See guideline recommendation 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 recommendation DCL00-C. Const-qualify immutable objects.

...

In this compliant solution, the macro definition is replaced with an enumeration constant in compliance with guideline recommendation 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 rule DCL37-C. Do not use identifiers that are reserved for the implementation.

...

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

Related Guidelines

CERT C++ Secure Coding Standard: PRE02-CPP. Macro replacement lists should be parenthesized

...

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

ISO/IEC PDTR 24772]\] "JCW Operator precedence/Order of Evaluation", "NMP Pre-processor Directions"

Bibliography

Wiki Markup

\[[Plum 1985|AA. Bibliography#Plum 85]\] Rule 1-1
\[[Summit 2005|AA. Bibliography#Summit 05]\] Question 10.1

...