An empty vector is indexed after reserve, which allocates memory but adds no elements
What it finds
std::vector<int> v; v.reserve(n); v[0] = 1;: reserve only sets aside memory, the vector still has no elements, and v[0] reaches past its end, which is undefined. resize makes the elements, or push_back adds them one by one.
The rule looks at a vector declared empty in a block, whose next use is reserve and whose use after that indexes it with [].
From the rule's source, index_after_reserve.rs.
Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/cpp:index-after-reserve" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/cpp:index-after-reserve] reason="..."tx0 explain tx0/cpp:index-after-reserve shows the same in the terminal. Found a false positive? Report it.