A for loop tests one variable and steps another, so its condition never changes

tx0/c:loop-steps-other-variabledefaultwarningreliability/control-flowdepth fastbug, cwe-835

What it finds

for (j = 0; j < m; i++): the condition tests one variable and the step changes another, and nothing in the loop changes the one tested, so the loop runs forever or not at all. In nested loops this is usually the outer counter typed by mistake.

The condition must compare a local variable or parameter with <, <=, >, >= or !=, the step increment or decrement another one, and neither the step nor the body may change the tested variable or anything its bound reads, nor may the function take its address.

From the rule's source, loop_steps_other_variable.rs.

Example from the rule's tests
void fill(int grid[4][4], int n, int m){  int i, j;  for (i = 0; i < n; i++)    for (j = 0; j < m; i++)tx0 reports: the loop tests j but its step changes i      grid[i][j] = 0;  for (i = 0; i < n; j++)    i = i + 2;  for (i = 0; i < j; j--)    grid[0][0] = i;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/c:loop-steps-other-variable" = "off"

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:loop-steps-other-variable] reason="..."

tx0 explain tx0/c:loop-steps-other-variable shows the same in the terminal. Found a false positive? Report it.