ITER.INAPPROPRIATEIterator with inappropriate container objectThe ITER checkers find problems with iterators in containers. The ITER.INAPPROPRIATE checker flags instances in which an iterator is assigned to one container and used with another container. Vulnerability and riskUsing an invalid iterator typically results in undefined behavior. For example, using the iterator in the wrong container can result in unpredictable program actions. Code in which an iterator is used in an inappropriate container always provides a false result, so the algorithm won't behave as expected or intended. Vulnerable code example1 void foo(set<int>& cont1, set<int>& cont2) 2 { 3 set<int>::iterator i = cont1.find(100); 4 if (i != cont1.end()) 5 cont2.erase(i); 6 } In this example, iterator 'i' is assigned to container 'cont1', and then used incorrectly with 'cont2', which will produce undefined results. Fixed code example1 void foo(set<int>& cont1, set<int>& cont2) 2 { 3 set<int>::iterator i = cont1.find(100); 4 if (i != cont1.end()) 5 { 6 i = cont2.find(100); 7 if (i != cont2.end()) 8 cont2.erase(i); 9 } In the fixed example, the correct iterator is retrieved for 'cont2'. Related checkersExtensionThis checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information. |