Problematic while(true) Loop

ID

java.loop_problematic_while

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports while(true) loops that contain no break, return, or throw statement.

Rationale

A while(true) loop with no exit path will run indefinitely. This is almost certainly a bug or an indication of missing termination logic. Even intentional infinite loops (such as event loops) should have an explicit exit mechanism guarded by a condition.

// Bad - no exit path
while (true) {
    processNextItem();
}

Remediation

Add a break, return, or throw statement inside the loop, or replace while(true) with a proper termination condition.

// Good - explicit exit
while (true) {
    Item item = getNext();
    if (item == null) {
        break;
    }
    process(item);
}

References