Loop with an empty body

ID

c.correctness.empty_loop_body

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Suspicious Construct

Language

C / C++

Description

This loop has an empty body (a stray ; immediately after the header). This is often an accidental typo that makes the intended loop body run only once, after the loop. If the empty body is intentional (a busy-wait), make it explicit with an empty block {} or a continue; and add a comment.

Rationale

This loop has an empty body (a stray ; immediately after the header). This is often an accidental typo that makes the intended loop body run only once, after the loop. If the empty body is intentional (a busy-wait), make it explicit with an empty block {} or a continue; and add a comment.

The following code illustrates the pattern detected by this rule:

void wait_flag(volatile int *flag) {
    // FLAGGED: Loop with an empty body
    while (*flag);

    // FLAGGED: Loop with an empty body
    for (int i = 0; i < 10; i++);
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.