V663. Infinite loop is possible. The 'cin.eof()' condition is insufficient to break from the loop. Consider adding the 'cin.fail()' function call to the conditional expression.


The analyzer has detected a potential error that may lead to an infinite loop. When you deal with the 'std::istream' class, calling the 'eof()' function is not enough to terminate the loop. If data reading fails, a call of the 'eof()' function will always return 'false'. You need an additional check of the value returned by the 'fail()' function to terminate the loop in this case.

Have a look at an example of incorrect code:

while (!cin.eof())
{
  int x;
  cin >> x;
}

You can fix the error by making the condition a bit more complex:

while (!cin.eof() && !cin.fail())
{ 
  int x;
  cin >> x;
}

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

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


Bugs Found

Checked Projects
355
Collected Errors
13 303