org.sonar.l10n.java.rules.squid.S2189.html Maven / Gradle / Ivy
An infinite loop is one that will never end while the program is running, I.E. you have to kill the program to get out of the loop. Whether it is by meeting the loop's end condition or via a break
, every loop should have an end condition.
Noncompliant Code Example
for (;;) { // Noncompliant; end condition omitted
// ...
}
for (int i = 0; i < 10; i--) { // Noncompliant; end condition but unreachedable
//...
}
int j;
while (true) { // Noncompliant; end condition omitted
j++;
}
int k;
boolean b = true;
while (b) { // Noncompliant; b never written to in loop
k++;
}
Compliant Solution
for (int i = 0; i < 10; i++) { // end condition now reachable
//...
}
int j;
while (true) { // reachable end condition added
j++;
if (j == Integer.MIN_VALUE) { // true at Integer.MAX_VALUE +1
break;
}
}
int k;
boolean b = true;
while (b) {
k++;
b = k < Integer.MAX_VALUE;
}
See
- CERT, MSC01-J. - Do not use an empty infinite loop
© 2015 - 2025 Weber Informatics LLC | Privacy Policy