If a while
or for
statement uses a loop counter, and increments or decrements it by more than one, it should use a numeric comparison operator (that is, <
, <=
, >
, or >=
) to terminate the loop. This prevents the loop from executing indefinitely or until the counter wraps around and reaches the final value. (See guideline rule NUM00-J. Detect or prevent integer overflow.)
...
Search for vulnerabilities resulting from the violation of this guideline rule on the CERT website.
Other Languages
This guideline rule appears in the C Secure Coding Standard as guideline rule MSC21-C. Use inequality to terminate a loop whose counter changes by more than one .
This guideline rule appears in the C++ Secure Coding Standard as guideline rule MSC21-CPP. Use inequality to terminate a loop whose counter changes by more than one.
...