ITR-end-cmp-aft (C++ only)
In this section:
Synopsis
An iterator is used, then compared with end()
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
An iterator is used, then compared with end(). Using an iterator requires that it does not point to the end of a container. Subsequently comparing it with end() or rend() means that it might have been invalid at the point of dereference.
Coding standards
- CERT ARR35-CPP
Do not allow loops to iterate beyond the end of an array or container
Code examples
The following code example fails the check and will give a warning:
#include <vector>
int example(std::vector<int>& vec,
std::vector<int>::iterator iter) {
*iter = 4; //line 9 asserts that iter may be
//at the end of vec
if (iter != vec.end()) {
return 0;
}
return 1;
}
The following code example passes the check and will not give a warning about this issue:
#include <vector>
int example(std::vector<int>& vec,
std::vector<int>::iterator iter) {
if (iter != vec.end()) {
*iter = 4;
}
if (iter != vec.end()) {
return 0;
}
return 1;
}