rspec/rules/S888/java/rule.adoc
Egon Okerman d1417e82f8
Modify CWE and OWASP Top 10 links to follow standard link format (APPSEC-1134) (#3529)
* Fix all CWE references

* Fix all OWASP references

* Fix missing CWE prefixes
2024-01-15 17:15:56 +01:00

68 lines
1.3 KiB
Plaintext

== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
[source,java]
----
for (int i = 1; i != 10; i += 2) // Noncompliant. Infinite; i goes from 9 straight to 11.
{
//...
}
----
=== Compliant solution
[source,java]
----
for (int i = 1; i <= 10; i += 2) // Compliant
{
//...
}
----
=== Exceptions
Equality operators are ignored if the loop counter is not modified within the body of the loop and either:
* starts below the ending value and is incremented by 1 on each iteration.
* starts above the ending value and is decremented by 1 on each iteration.
Equality operators are also ignored when the test is against ``++null++``.
[source,java]
----
for (int i = 0; arr[i] != null; i++) {
// ...
}
for (int i = 0; (item = arr[i]) != null; i++) {
// ...
}
----
== Resources
* CWE - https://cwe.mitre.org/data/definitions/835[CWE-835 - Loop with Unreachable Exit Condition ('Infinite Loop')]
* https://wiki.sei.cmu.edu/confluence/x/x9YxBQ[CERT, MSC21-C.] - Use robust loop termination conditions
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]