Skip to content

Commit

Permalink
Add bounds checking on List access.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Nov 1, 2024
1 parent 4d84811 commit fd5b8d1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/std/collections/list.c3
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn Type! List.pop_first(&self)
}

<*
@require index < self.size
@require index < self.size `Removed element out of bounds`
*>
fn void List.remove_at(&self, usz index)
{
Expand Down Expand Up @@ -215,7 +215,7 @@ fn void List.push_front(&self, Type type) @inline
}

<*
@require index <= self.size
@require index <= self.size `Insert was out of bounds`
*>
fn void List.insert_at(&self, usz index, Type type)
{
Expand Down Expand Up @@ -275,6 +275,9 @@ fn usz List.len(&self) @operator(len) @inline
return self.size;
}

<*
@require index < self.size `Access out of bounds`
*>
fn Type List.get(&self, usz index) @inline
{
return self.entries[index];
Expand All @@ -296,6 +299,9 @@ fn void List.free(&self)
self.entries = null;
}

<*
@require i < self.size && j < self.size `Access out of bounds`
*>
fn void List.swap(&self, usz i, usz j)
{
@swap(self.entries[i], self.entries[j]);
Expand Down Expand Up @@ -358,16 +364,25 @@ fn void List.ensure_capacity(&self, usz min_capacity) @local
self.post_alloc(); // Add sanitizer annotation
}

<*
@require index < self.size `Access out of bounds`
*>
macro Type List.@item_at(&self, usz index) @operator([])
{
return self.entries[index];
}

<*
@require index < self.size `Access out of bounds`
*>
fn Type* List.get_ref(&self, usz index) @operator(&[]) @inline
{
return &self.entries[index];
}

<*
@require index < self.size `Access out of bounds`
*>
fn void List.set(&self, usz index, Type value) @operator([]=)
{
self.entries[index] = value;
Expand Down

0 comments on commit fd5b8d1

Please sign in to comment.