tripon[github]

diagnostic codes / Traps

E9002 index-out-of-bounds

An index was outside the array.

E9002index-out-of-boundsindexing is bounds-checked everywhere; slice.len() before indexing
severitytrap
bandTraps
in the v0 bootstrapemitted
explainralt explain E9002

Array indexing is checked everywhere. A constant index outside the length is caught at check time as E0202. An index that is only known at run time is checked at the access and traps with E9002. A `str` index counts bytes, and the same rule applies.

What it looks like

e9002.tp
use tier core;

fn main() -> i32 {
    let a: [3]i32;
    let i = 3;
    print_i32(a[i]);
    return 0;
}
ralt run e9002.tp
tripon trap E9002 index-out-of-bounds at e9002.tp:6
echo $?
101

`let i = 3;` makes the index opaque to the checker, so this one reaches the runtime. With a literal `a[3]` the same program is refused before codegen.

What to do

  • Compare the index with `len(a)` before the access when the index comes from input.
  • Remember the valid range is `0 .. len - 1`; `len(a)` is one past the last element.
  • An empty array has no valid index, including `0`.

Why the code exists

An out-of-bounds read is the classic memory-safety bug, and a bounds check is one compare. The trade is explicit: pay the compare, or lose the guarantee.

What ralt explain says

ralt explain E9002
E9002
severity: error
name: index-out-of-bounds
hint: indexing is bounds-checked everywhere; slice.len() before indexing

Related