Main Content

CERT C: Rec. EXP19-C

Use braces for the body of an if, for, or while statement

Description

Rule Definition

Use braces for the body of an if, for, or while statement.1

Polyspace Implementation

The rule checker checks for Iteration or selection statement body not enclosed in braces.

Examples

expand all

Issue

The issue occurs when you do not enclose the body of an iteration-statement or a selection-statement in braces.

Risk

The rule applies to:

  • Iteration statements such as while, do ... while or for.

  • Selection statements such as if ... else or switch.

If the block of code associated with an iteration or selection statement is not contained in braces, you can make mistakes about the association. For example:

  • You can wrongly associate a line of code with an iteration or selection statement because of its indentation.

  • You can accidentally place a semicolon following the iteration or selection statement. Because of the semicolon, the line following the statement is no longer associated with the statement even though you intended otherwise.

Example - Iteration Block
int data_available = 1;
void f1(void) {
    while(data_available)                 /* Non-compliant */
        process_data();

    while(data_available) {               /* Compliant */
        process_data();
    }
}

In this example, the second while block is enclosed in braces and does not violate the rule.

Example - Nested Selection Statements
#include <stdbool.h>

bool flag_1, flag_2;

void f1(void) {
    if(flag_1)                            /* Non-compliant */
        if(flag_2)                        /* Non-compliant */
            action_1();
    else                                  /* Non-compliant */
            action_2();
}

In this example, the rule is violated because the if or else blocks are not enclosed in braces. Unless indented as above, it is easy to associate the else statement with the inner if.

Correction — Place Selection Statement Block in Braces

One possible correction is to enclose each block associated with an if or else statement in braces.

#include <stdbool.h>

bool flag_1, flag_2;

void f1(void) {
    if(flag_1) {                          /* Compliant */
        if(flag_2) {                        /* Compliant */
            action_1();
        }
    }
    else {                                /* Compliant */
        action_2();
    }
}

Example - Spurious Semicolon After Iteration Statement
#include <stdbool.h>

bool flag_1;

void f1(void) {
    while(flag_1);                        /* Non-compliant */
    {
        flag_1 = action_1();
    }
}

In this example, the rule is violated even though the while statement is followed by a block in braces. The semicolon following the while statement causes the block to dissociated from the while statement.

The rule helps detect such spurious semicolons.

Check Information

Group: Rec. 03. Expressions (EXP)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.