Main Content

CWE Rule 1071

Empty code block

Since R2023a

Description

Rule Definition

The source code contains a block that does not contain any code, i.e., the block is empty.

Polyspace Implementation

This rule checker checks for the issue Empty Code Block.

Examples

expand all

Issue

Empty Code Block occurs when a code block does not contain code.

Risk

Unintentional empty code block can occur if you do not complete implementation or if you accidentally delete code. Unintentional missing code can cause errors or unexpected results.

Fix

Remove the empty code block if possible. Fill in the missing or deleted code if the code block is unintentionally empty due to accidental code deletion or incomplete implementation. If you can't remove or implement missing code, an empty code block can be replaced by an empty instruction. For example:

while (true)
;

Example — Empty Code Block

In this example, the inner for loop code block is empty. The example shows a programming error where the developer probably wanted to enter the addition in the inner loop but added it in the outer loop by mistake.

int addAllElements(int *arr, int xSize, int ySize) {
    int sum = 0, i, j;
    for (i = 0; i < xSize; i++) {
        sum += *(arr + i * xSize + i);
        for (j = 0; j < ySize; j++) { //Noncompliant
            
        }
    }
    return sum;
}
Correction — Add Missing Code

In this example, you can fix the issue by moving the operation from the outer to the inner block.

int addAllElements(int *arr, int xSize, int ySize) {
    int sum = 0, i, j;
    for (i = 0; i < xSize; i++) {
        for (j = 0; j < ySize; j++) { //Compliant
           sum += *(arr + i * xSize + i);
        }
    }
    return sum;
}

Check Information

Category: Bad Coding Practices

Version History

Introduced in R2023a