V3150. Loop break conditions do not depend on the number of iterations.


The analyzer has detected a loop whose termination conditions do not depend on the number of iterations. Such a loop can iterate 0, 1, or an infinite number of times.

Consider the following example of such a loop:

void Foo(int left, int right)
{
  while(left < right) 
  { 
    Bar();
  }
}

The problem is with the while loop: the variables being checked in the condition do not change; therefore, the loop will either never terminate or never start.

Here is another example of code that would trigger this diagnostic. A loop may become infinite if you forget to rethrow an exception in the 'try-catch' block down the stack:

while (condition)
{
  try {
    if(Foo())
    {
       throw new Exception();
    }
  }
  catch (Exception ex) 
  { 
    ....
  }
}

To have this loop terminate on throwing the exception, you can, for example, rethrow this exception from the catch section using the throw statement:

while (condition)
{
  try {
    if(Foo())
    {
      throw new Exception();
    }
  }
  catch (Exception ex) 
  { 
    ....      
    throw; 
  }
}

Bugs Found

Checked Projects
372
Collected Errors
13 634