Skip to content

Commit ef21617

Browse files
martinvonzByron
authored andcommitted
make ItemSliceSync::get_mut() check that the index is in range
This removes some of the unsafety from ItemSliceSync::get_mut(). There's still the unsafety that the caller needs to make sure a single index is not used concurrently.
1 parent e6c705a commit ef21617

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

gix-pack/src/cache/delta/traverse/resolve.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ mod node {
5757
/// Children are `Node`s referring to pack entries whose base object is this pack entry.
5858
pub fn into_child_iter(self) -> impl Iterator<Item = Node<'a, T>> + 'a {
5959
let children = self.child_items;
60-
// SAFETY: The index is a valid index into the children array.
6160
// SAFETY: The resulting mutable pointer cannot be yielded by any other node.
6261
#[allow(unsafe_code)]
6362
self.item.children.iter().map(move |&index| Node {

gix-pack/src/cache/delta/traverse/util.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ where
55
T: Send,
66
{
77
items: *mut T,
8+
len: usize,
89
phantom: PhantomData<&'a T>,
910
}
1011

@@ -15,13 +16,18 @@ where
1516
pub fn new(items: &'a mut [T]) -> Self {
1617
ItemSliceSync {
1718
items: items.as_mut_ptr(),
19+
len: items.len(),
1820
phantom: PhantomData,
1921
}
2022
}
2123

22-
/// SAFETY: The index must point into the slice and must not be reused concurrently.
24+
// SAFETY: The index must not be reused concurrently
2325
#[allow(unsafe_code)]
2426
pub unsafe fn get_mut(&self, index: usize) -> &'a mut T {
27+
if index >= self.len {
28+
panic!("index out of bounds: the len is {} but the index is {index}", self.len);
29+
}
30+
// SAFETY: The index is within the slice
2531
// SAFETY: The children array is alive by the 'a lifetime.
2632
unsafe { &mut *self.items.add(index) }
2733
}

0 commit comments

Comments
 (0)