Boundary checks on lists do not work

I noticed that the following code passes compilation and returns a value that is out of the boundary of a list:

def main():
    var lst = List[Int](1, 2, 3, 4, 5)
    var item = lst[10]
    print("Item at index 10:", item)

This prints:

Item at index 10: 5102179120

My memory might be wrong, but I remembered that in an earlier Mojo version, this would lead to a compilation error.

I also checked the standard library and see that there is indeed a boundary check for the List type:

fn __getitem__[I: Indexer](ref self, idx: I) -> ref [self] T:
    @parameter
    if _type_is_eq[I, UInt]():
        return (self.data + idx)[]
    else:
        var normalized_idx = Int(idx)
        debug_assert(
            -self._len <= normalized_idx < self._len,
            "index: ",
            normalized_idx,
            " is out of bounds for `List` of length: ",
            self._len,
        )
        if normalized_idx < 0:
            normalized_idx += len(self)

        return (self.data + normalized_idx)[]

Is there anything wrong with my code so the the boundary checks are by-passed?

Bound checks are off by default. You can enable them by adding '-D ASSERT=all` and then indexing out of bounds (on CPU) would error. Try this:

mojo -D ASSERT=all yourfile.mojo

Documentation page: debug_assert | Modular

Note that this is not a compilation error but a run-time error.

Thank you @denis !

I am wondering whether boundary checks will be on by default in the future, since it is a “safe” feature?

Yes, we are considering turning bounds checks on by default. The challenge is to make the compiler smart enough, so that compilation time and speed of compiled code do not degrade too much when bounds checks are enabled. There is some work the Mojo team needs to do first.

2 Likes

Thank you very much for your answer. I will keep this roadmap in mind and play safely with the list type at the current stage. :smiley:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.