Versions Compared

Key

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

...

More importantly, when inserting an additional statement into a body containing only a single statement, it is easy to forget to add braces because the indentation gives strong (but misleading) guidance to the structure.

Noncompliant Code Example

This noncompliant code example uses an if statement without braces to authenticate the user.

...

The code indentation disguises the functionality of the program, potentially leading to a security breach.

Compliant Solution

In this compliant solution, opening and closing braces are used even when the body is a single statement.

Code Block
bgColor#CCCCFF
int login;

if (invalid_login()) {
  login = 0;
} else {
  login = 1;
}

Noncompliant Code Example

This noncompliant code example nests an if statement within another if statement without braces around if and else bodies.

...

This is a vulnerability because unauthorized users can obtain administrator privileges.

Compliant Solution

In this compliant solution, adding braces removes the ambiguity and ensures that privileges are correctly assigned.

Code Block
bgColor#CCCCFF
int privileges;

if (invalid_login()) {
  if (allow_guests()) {
    privileges = GUEST;
  } 
} else {
  privileges = ADMINISTRATOR;
}

Risk Assessment

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

EXP05-J

medium

probable

medium

P8

L2

Related Guidelines

CERT C Secure Coding Standard: "EXP19-C. Use braces for the body of an if, for, or while statement"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b0158b5b003f7cd0-ca022849-452249b9-8d359e1c-924d129e1ffd786592cd4835"><ac:plain-text-body><![CDATA[

[[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]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="26eaceb35876d091-54ab3108-45874949-86538bf2-7e61cd406a794d2479ec63a0"><ac:plain-text-body><![CDATA[

[[Rogue 2000

AA. Bibliography#Rogue 2000]]

Rule 76: Use block statements instead of expression statements in control flow constructs

]]></ac:plain-text-body></ac:structured-macro>

...