...
If an if
, while
, or for
statement is used in a macro, then the macro definition should not be concluded with a semicolon. (see See guideline PRE11-C. Do not conclude macro definitions with a semicolon.).
Braces help improve the uniformity and readability of code.
...
Braces also help ensure that macros with multiple statements are properly expanded. Such a macro should be wrapped in a do-while loop. (see See guideline PRE10-C. Wrap multi-statement macros in a do-while loop.) ; howeverHowever, when the do-while loop is not present, braces can still ensure that the macro expands as intended.
...
However, in reality, the else
statement actually attaches to the inner if
statement, like so:
Code Block | ||
---|---|---|
| ||
int privileges; if (invalid_login()) if (allow_guests()) privileges = GUEST; else privileges = ADMINISTRATOR; |
This is a security loophole - users loopholeâ”users with invalid logins can still obtain administrator privileges.
...
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 6.8.4, "Selection statements" \[[MISRA 042004|AA. Bibliography#MISRA 04]\] Rule 14.8 \[[GNU Coding Standards|http://www.gnu.org/prep/standards/standards.html#Syntactic-Conventions]\] Section 5.3, "Clean Use of C Constructs" |
...