while loop whose body never assigns and cannot exit
ID |
scala.correctness.loop_var_not_updated |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Infinite Loop |
Language |
Scala |
Description
Reports a while loop guarded by a comparison on a local variable whose body contains no assignment and no return, throw or break, so nothing can ever make the condition false.
Rationale
The loop spins forever on the first iteration’s state. Because it neither fails nor logs, the symptom is a wedged thread and a pegged core, usually diagnosed only from a thread dump — and in a request handler or an actor it removes that thread from the pool for the life of the process. The mistake is nearly always a forgotten increment, so the intended behaviour is one line away.
The following code illustrates the pattern detected by this rule:
class Client {
def ping(): Boolean = true
def next(): String = ""
}
class Retry(client: Client) {
def pollForever(attempts: Int): Unit = {
var i = 0
// FLAGGED: while loop whose body never assigns and cannot exit
while (i < attempts) {
client.ping()
}
}
}