Skip to content

Commit c27e72e

Browse files
author
Your Name
committed
Add documentation for checker
1 parent 0dadc9f commit c27e72e

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# algorithmOutOfBounds
2+
3+
**Message**: The algorithm 'std::copy' accesses 5 elements through the iterator 'v1.begin()' but only 3 elements are available.<br/>
4+
**Category**: Correctness<br/>
5+
**Severity**: Error<br/>
6+
**Language**: C++
7+
8+
## Description
9+
10+
Many STL algorithms take an iterator that denotes the beginning of a second range (typically an output range) and
11+
assume that this range is large enough. If it is not, the algorithm writes or reads past the end of the container,
12+
which is undefined behavior.
13+
14+
This checker uses the ValueFlow analysis to compare the number of elements an algorithm accesses with the number of
15+
elements that are actually available through the iterator, and warns when the access is out of bounds. Three groups
16+
of algorithms are checked:
17+
18+
- Algorithms that access exactly `last1 - first1` elements through the other iterator: `std::copy`, `std::move`,
19+
`std::swap_ranges`, `std::transform`, `std::replace_copy`, `std::replace_copy_if`, `std::reverse_copy`,
20+
`std::equal`, `std::mismatch`, `std::is_permutation`, `std::partial_sum`, `std::adjacent_difference` and
21+
`std::inner_product`.
22+
- Algorithms that access at most `last1 - first1` elements, depending on the values in the input range:
23+
`std::copy_if`, `std::remove_copy`, `std::remove_copy_if` and `std::unique_copy`. Since the actual number of
24+
accessed elements is not known, these are only reported as inconclusive warnings (with `--inconclusive`).
25+
- Count-based algorithms that access as many elements as the count argument says: `std::copy_n`, `std::fill_n` and
26+
`std::generate_n`.
27+
28+
The severity is `error` when the out of bounds access always happens. When the analysis depends on an earlier
29+
condition in the code, the severity is `warning` and the message has the form "Either the condition 'v.size()==3'
30+
is redundant or the algorithm ... ".
31+
32+
The checker does not warn when:
33+
34+
- The second range is given with both a begin and an end iterator (for example the two-range overloads of
35+
`std::equal`, `std::mismatch` and `std::is_permutation`), since such overloads do not access the second range out
36+
of bounds.
37+
- An iterator adaptor such as `std::back_inserter` or `std::inserter` is used, since those grow the container as
38+
needed.
39+
- The proof would rely on "possible" (non-known) values on both the accessed and the available side.
40+
41+
## How to fix
42+
43+
Make sure the destination range is large enough before calling the algorithm, or use an iterator adaptor such as
44+
`std::back_inserter` that grows the container as needed.
45+
46+
Before:
47+
```cpp
48+
void f(const std::vector<int>& v0) {
49+
std::vector<int> v1(3);
50+
// If v0 has more than 3 elements, this writes past the end of v1
51+
std::copy(v0.begin(), v0.end(), v1.begin());
52+
}
53+
```
54+
55+
After:
56+
```cpp
57+
void f(const std::vector<int>& v0) {
58+
std::vector<int> v1(v0.size());
59+
std::copy(v0.begin(), v0.end(), v1.begin());
60+
}
61+
```
62+
63+
Or let the container grow:
64+
```cpp
65+
void f(const std::vector<int>& v0) {
66+
std::vector<int> v1;
67+
std::copy(v0.begin(), v0.end(), std::back_inserter(v1));
68+
}
69+
```

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Major bug fixes & crashes:
77
New checks:
88
- Warn when feof() is used as a while loop condition (wrongfeofUsage).
99
- ftell() result is unspecified when file is opened in mode "t".
10+
- Detect when an STL algorithm such as std::copy, std::equal, std::transform, etc. accesses more elements through an iterator than are available in the container (algorithmOutOfBounds).
1011

1112
C/C++ support:
1213
-

0 commit comments

Comments
 (0)