...
- Both iterators refer into the same container.
- The iterator representing the start of the range precedes the iterator representing the end of the range.
- The elements iterated over do not have unspecified values.The iterators are not invalidated, in conformance with CTR51-CPP. Use valid references, pointers, and iterators to reference elements of a container.
An empty iterator range (where the two iterators are valid and equivalent) is considered to be valid.
Accessing Using a range of two iterators that are invalidated or do not refer into the same container or accessing invalidated iterators results in undefined behavior.Several generic standard template library (STL) algorithms, such as std::remove()
and std::unique()
, remove instances of elements from a container without shrinking the size of the container. Instead, these algorithms return a ForwardIterator
to indicate the partition within the container after which elements are no longer valid. The elements in the container that precede the returned iterator are valid elements with specified values, whereas the elements that succeed the returned iterator are valid but have unspecified values. Accessing unspecified values of elements iterated over results in unspecified behavior. Frequently, the erase-remove idiom is used to shrink the size of the container when using these algorithms.
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <algorithm>
#include <iostream>
#include <vector>
void f(const std::vector<int> &c) {
std::for_each(c.begin(), c.end(), [](int i) { std::cout << i; });
}
|
Noncompliant Code Example
In this noncompliant code example, elements matching 42
are removed from the given container. The contents of the container are then printed to the standard output stream. However, if any elements were removed from the container, the range-based for
loop iterates over an invalid iterator range, resulting in unspecified behavior.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <algorithm> #include <iostream> #include <vector> void f(std::vector<int> &c) { std::remove(c.begin(), c.end(), 42); for (auto v : c) { std::cout << "Container element: " << v << std::endl; } } |
Compliant Solution
In this compliant solution, elements removed by the standard algorithm are subsequently erased from the given container. This technique ensures that a valid iterator range is used by the range-based for
loop.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <algorithm>
#include <iostream>
#include <vector>
void f(std::vector<int> &c) {
c.erase(std::remove(c.begin(), c.end(), 42), c.end());
for (auto v : c) {
std::cout << "Container element: " << v << std::endl;
}
} |
Risk Assessment
Using an invalid iterator range is similar to allowing a buffer overflow, which can lead to an attacker running arbitrary code.
...