<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61544>61544</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clangsa alpha.cplusplus.IteratorRange unsound on trivial example
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
peckto
</td>
</tr>
</table>
<pre>
The [alpha.cplusplus.IteratorRange](https://clang.llvm.org/docs/analyzer/checkers.html#alpha-cplusplus-iteratorrange-c) checker is missing the following trivial example:
```c++
#include <vector>
int main() {
std::vector<int> v;
v.erase(v.begin(), v.begin() + 5);
return 0;
}
```
` v.begin() + 5` is clearly past-the-end iterator.
Whereas, in the following example the bug is reported:
```c++
#include <vector>
int main() {
std::vector<int> v;
for (auto &c : v) {
}
v.erase(v.begin(), v.begin() + 5);
return 0;
}
```
```
$ clang++-16 --analyze -Xclang -analyzer-config -Xclang aggressive-binary-operation-simplification=true -Xanalyzer -analyzer-checker=alpha.cplusplus main.cpp
main.cpp:7:24: warning: Iterator incremented behind the past-the-end iterator [alpha.cplusplus.IteratorRange]
v.erase(v.begin(), v.begin() + 5);
^~~~~~~~~~~~~
```
(Same behavior when running via CodeChecker)
I have no clue why for loop is necessary to find the bug.
Maybe it's too trivial and clangsa thinks, the variable is not used and cut by the optimizer?
When executing, both example crash as expected with `Segmentation fault`.
Tested with clang-14 and clang-16:
```
Debian clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
```
```
Debian clang version 16.0.0 (++20230317013132+08d094a0e457-1~exp1~20230317133238.60)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVs9u4zYTfxr6MpBAkbIsH3xw7DWwh-_y7QLtraCoscSGIgWSku0e8uwFZVmbZNNtD8U2UGzP_-HM_E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