V3116. Consider inspecting the 'for' operator. It's possible that the loop will be executed incorrectly or won't be executed at all.


The analyzer detected a 'for' statement with incorrect bounds of the iterator. Consider the following example:

for (int i = 0; i < 100; --i)

This code is obviously incorrect: the value of the 'i' variable will always be less than 100, at least until it overflows. This behavior is hardly what the programmer expected. To fix this error, we need either to replace the decrement operation '--i' with increment operation '++i':

for (int i = 0; i < 100; ++i)

or to specify the appropriate bounds for the 'i' variable using the relational operator '>' or '!= ':

for (int i = 99; i >= 0; --i)
for (int i = 99; i != -1; --i)

Which solution is the right one is up to the author of the code to decide depending on the particular situation.

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-835.


Bugs Found

Checked Projects
344
Collected Errors
12 970