A for loop steps its counter away from the bound its condition tests
What it finds
for (i = 0; i < n; i--): the counter moves away from the bound its condition tests, so the loop runs until the counter overflows, which for a signed counter is undefined.
The counter must be a local variable or parameter of a signed integer type the file declares, compared with <, <=, > or >=, and stepped with ++, --, += or -= a positive literal; the body may not change it. Unsigned counters are left alone, since counting down past zero to stop at a bound is a known idiom with them.
From the rule's source, wrong_loop_direction.rs.
void clear(int *a, int n, unsigned u){ int i; unsigned k; for (i = 0; i < n; i--)tx0 reports: i counts down, away from the bound a[i] = 0; for (i = n; i >= 0; i++)tx0 reports: i counts up a[i] = 1; for (k = u - 1; k < u; k--) a[k] = 2; for (i = n - 1; i >= 0; i--) a[i] = 3;} 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/c:wrong-loop-direction" = "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/c:wrong-loop-direction] reason="..."tx0 explain tx0/c:wrong-loop-direction shows the same in the terminal. Found a false positive? Report it.