An array is indexed with a constant outside its bounds

tx0/c:constant-index-out-of-boundsdefaultwarningbug, cwe-787

What it finds

int a[4]; a[4] = 0;: the index is a constant at or past the length of the array, or negative, so the access is outside the array. Taking the address one past the end, as &a[4], is allowed and left alone, as is anything under sizeof.

The array must be a variable, not a parameter, declared in the file with a literal length; the index must be written with literals.

From the rule's source, constant_index_out_of_bounds.rs.

Example from the rule's tests
int table[4]; int pick(int *p){  int local[2][3];  int *end = &table[4];  local[1][2] = 0;  local[2][0] = 1;tx0 reports: index 2 is outside local, which has 2 elements  p[9] = 0;  return table[4] + (end - p) + (int)sizeof(table[10]);tx0 reports: index 4 is outside table, which has 4 elements}
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:constant-index-out-of-bounds" = "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:constant-index-out-of-bounds] reason="..."

tx0 explain tx0/c:constant-index-out-of-bounds shows the same in the terminal. Found a false positive? Report it.