2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-27 15:03:47 +02:00
A `for` loop with a stop condition that can never be reached, such as one with a counter that moves in the wrong direction, will run infinitely. While there are occasions when an infinite loop is intended, the convention is to construct such loops as `while` loops. More typically, an infinite `for` loop is a bug.
2020-06-30 12:48:07 +02:00
2023-06-27 15:03:47 +02:00
== How to fix it
2020-06-30 12:48:07 +02:00
2023-06-27 15:03:47 +02:00
=== Code examples
==== Noncompliant code example
[source,javascript,diff-id=1,diff-type=noncompliant]
2020-06-30 12:48:07 +02:00
----
2023-06-27 15:03:47 +02:00
for (var i = 0; i < strings.length; i--) { // Noncompliant
2020-06-30 12:48:07 +02:00
//...
}
----
2023-06-27 15:03:47 +02:00
==== Compliant solution
2020-06-30 12:48:07 +02:00
2023-06-27 15:03:47 +02:00
[source,javascript,diff-id=1,diff-type=compliant]
2020-06-30 12:48:07 +02:00
----
for (var i = 0; i < strings.length; i++) {
//...
}
----
2023-06-27 15:03:47 +02:00
== Resources
2021-06-02 20:44:38 +02:00
2023-06-27 15:03:47 +02:00
=== Documentation
2021-06-02 20:44:38 +02:00
2023-06-27 15:03:47 +02:00
* https://en.wikipedia.org/wiki/Integer_overflow[Integer overflow]
2023-05-25 14:18:12 +02:00
2023-06-27 15:03:47 +02:00
include::../rspecator.adoc[]