Skip to content

Commit

Permalink
Demote ItemSliceMut::get_mut() bound-check to debug-code only.
Browse files Browse the repository at this point in the history
By now we are certain the used indices are in bounds, but if they are
not this would not be detected by the test-suite first.
  • Loading branch information
Byron committed Jan 3, 2024
1 parent ef21617 commit 911c05f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions gix-pack/src/cache/delta/traverse/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod node {
/// Children are `Node`s referring to pack entries whose base object is this pack entry.
pub fn into_child_iter(self) -> impl Iterator<Item = Node<'a, T>> + 'a {
let children = self.child_items;
// SAFETY: The index is a valid index into the children array.
// SAFETY: The resulting mutable pointer cannot be yielded by any other node.
#[allow(unsafe_code)]
self.item.children.iter().map(move |&index| Node {
Expand Down
5 changes: 4 additions & 1 deletion gix-pack/src/cache/delta/traverse/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ where
T: Send,
{
items: *mut T,
#[cfg(debug_assertions)]
len: usize,
phantom: PhantomData<&'a T>,
}
Expand All @@ -16,14 +17,16 @@ where
pub fn new(items: &'a mut [T]) -> Self {
ItemSliceSync {
items: items.as_mut_ptr(),
#[cfg(debug_assertions)]
len: items.len(),
phantom: PhantomData,
}
}

// SAFETY: The index must not be reused concurrently
// SAFETY: The index must point into the slice and must not be reused concurrently.
#[allow(unsafe_code)]
pub unsafe fn get_mut(&self, index: usize) -> &'a mut T {
#[cfg(debug_assertions)]
if index >= self.len {
panic!("index out of bounds: the len is {} but the index is {index}", self.len);
}
Expand Down

0 comments on commit 911c05f

Please sign in to comment.