V3020. An unconditional 'break/continue/return/goto' within a loop.


The analyzer has detected a suspicious loop where one of the following statements is used: continue, break, return, goto, or throw. These statements are executed all the time, irrespective of any conditions. For example:

while (k < max)
{                
  if (k == index)
    value = Calculate(k);
  break;
  ++k;
}

In this code, the 'break' statement doesn't belong to the 'if' statement, which will cause it to execute all the time, regardless of whether or not the 'k == index' condition is true, and the loop body will iterate only once. The correct version of this code should look like this:

while (k < max)
{                
  if (k == index)
  {
    value = Calculate(k);
    break;
  }
  ++k;
}

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

You can look at examples of errors detected by the V3020 diagnostic.


Bugs Found

Checked Projects
334
Collected Errors
12 668