Opening Use opening and closing braces for if
, for
, or and while
statements should always be used, even if when the statement's body contains only a single statement.If an if
, while
, or for
statement is used in a macro, the macro definition should not conclude with a semicolon. (See recommendation PRE11-C. Do not conclude macro definitions with a semicolon. ) Braces improve the uniformity and readability of code.
More importantly, when inserting an additional statement important, it is easy to forget to add braces when inserting additional statements into a body containing only a single statement, it is easy to forget to add braces because the conventional indentation gives strong (but misleading) guidance to the structure.
Braces also help ensure that macros with multiple statements are properly expanded. Such a macro should be wrapped in a do-while loop. (See recommendation PRE10-C. Wrap multi-statement macros in a do-while loop.) However, when the do-while loop is not present, braces can still ensure that the macro expands as intended.
Noncompliant Code Example
This noncompliant code example uses authenticates a user with an if
statement without braces to authenticate a user.that lacks braces:
Code Block | ||
---|---|---|
| ||
int login;
if (invalid_login())
login = 0;
else
login = 1;
|
A developer might This program behaves as expected. However, a maintainer might subsequently add a debugging statement to determine when the login is valid, debug statement or other logic but forget to add opening and closing braces.:
Code Block | ||
---|---|---|
| ||
int login; if (invalid_login()) login = 0; else // Debug line added below printfSystem.out.println("Login is valid\n"); /*/ The debuggingnext line addedis always hereexecuted */ login login = 1; /* this line always gets executed, regardless of a valid login! */ |
The code's indentation disguises the functionality of the programDue to the indentation of the code, it is difficult to tell that the code will not function as intended by the programmer, potentially leading to a security breach.
Compliant Solution
In the This compliant solution , uses opening and closing braces are used even when though the body is a single statement.of the if
and else
bodies of the if statement are single statements:
Code Block | ||
---|---|---|
| ||
int login;
if (invalid_login()) {
login = 0;
} else {
login = 1;
}
|
Noncompliant Code Example
This noncompliant code example has nests an if
statement nested in within another if
statement, without braces around the if
and else
bodies.:
Code Block | ||
---|---|---|
| ||
int privileges;
if (invalid_login())
if (allow_guests())
privileges = GUEST;
else
privileges = ADMINISTRATOR;
|
The indentation could might lead the programmer to believe that a user is given users are granted administrator privileges only when the user's their login is valid. However, the else
statement actually attaches binds to the inner if
statement:
Code Block | ||
---|---|---|
| ||
int privileges;
if (invalid_login())
if (allow_guests())
privileges = GUEST;
else
privileges = ADMINISTRATOR;
|
This is a security loophole: users with invalid logins can still Consequently, this defect allows unauthorized users to obtain administrator privileges.
Compliant Solution
In the This compliant solution , adding uses braces removes to remove the ambiguity and ensures , consequently ensuring that privileges are correctly assigned.:
Code Block | ||
---|---|---|
| ||
int privileges;
if (invalid_login()) {
if (allow_guests()) {
privileges = GUEST;
}
} else {
privileges = ADMINISTRATOR;
}
|
Risk Assessment
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP19-C | medium | probable | medium | P8 | L2 |
Related Guidelines
ISO/IEC 9899:1999 Section 6.8.4, "Selection statements"
MISRA Rule 14.8
Bibliography
Wiki Markup |
---|
\[[GNU 2010|AA. Bibliography#GNU 10]\] [Coding Standards, Section 5.3, "Clean Use of C Constructs"|http://www.gnu.org/prep/standards/standards.html#Syntactic-Conventions] |
Applicability
Failure to enclose the bodies of if
, for
, or while
statements in braces makes code error prone and increases maintenance costs.
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.EXP52.BLK | Provide a '{}' block for conditional statements | ||||||
PVS-Studio |
| V6089 | |||||||
SonarQube |
|
Bibliography
...
EXP18-C. Do not perform assignments in selection statements 03. Expressions (EXP) EXP20-C. Perform explicit tests to determine success, true and false, and equality