V662. Consider inspecting the loop expression. Different containers are utilized for setting up initial and final values of the iterator.


The analyzer has detected a suspicious loop. The A container is used to initialize the iterator. Then this iterator is compared to the end of the B container. It's highly probable that it is a misprint and the code is incorrect.

Here is a sample for which this warning will be generated:

void useVector(vector<int> &v1, vector<int> &v2)
{
  vector<int>::iterator it;
  for (it = v1.begin(); it != v2.end(); ++it)
    *it = rand(); 
  ....
}

The array is being filled in the 'for' loop. Different variables (v1 and v2) are used to initialize the iterator and to check the bounds. If the references v1 and v2 actually point to different arrays, it will cause an error at the program execution stage.

The error is very easy to fix. You need to use one and the same container in the both cases. This is the fixed code:

void useVector(vector<int> &v1, vector<int> &v2)
{
  vector<int>::iterator it;
  for (it = v1.begin(); it != v1.end(); ++it)
    *it = rand();
 ....
}

If the variables v1 and v2 refer to one and the same container, the code is correct. You can use the false positive suppression mechanism of analyzer in this case. However, code refactoring seems a better solution to this issue. The current code may confuse not only the analyzer, but also those programmers who will maintain it in the future.

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

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


Bugs Found

Checked Projects
364
Collected Errors
13 504