Skip to content

Commit 7f7a058

Browse files
committed
patina_internal_collections: Prevent out-of-bounds access in resize()
Add bounds checking before accessing buffer[idx] in Storage::resize(). When resizing to a buffer with no free space (idx >= buffer.len()), set available to null instead of attempting to access out-of-bounds memory. Signed-off-by: Michael Kubacki <[email protected]>
1 parent 3cf95e9 commit 7f7a058

File tree

1 file changed

+29
-2
lines changed
  • core/patina_internal_collections/src

1 file changed

+29
-2
lines changed

core/patina_internal_collections/src/node.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,12 @@ where
240240

241241
let idx = if !self.available.get().is_null() { self.idx(self.available.get()) } else { self.len() };
242242

243-
Self::build_linked_list(&buffer[idx..]);
244-
self.available.set(buffer[idx].as_mut_ptr());
243+
if idx < buffer.len() {
244+
Self::build_linked_list(&buffer[idx..]);
245+
self.available.set(buffer[idx].as_mut_ptr());
246+
} else {
247+
self.available.set(core::ptr::null_mut());
248+
}
245249

246250
self.data = buffer;
247251
}
@@ -700,6 +704,29 @@ mod tests {
700704
assert!(Node::successor(p4).is_none());
701705
}
702706

707+
#[test]
708+
fn test_resize_with_no_free_space() {
709+
const CAPACITY: usize = 5;
710+
let mut memory = [0; CAPACITY * node_size::<usize>()];
711+
let mut storage = Storage::<usize>::with_capacity(&mut memory);
712+
713+
// Fill all the storage
714+
for i in 0..CAPACITY {
715+
storage.add(i).unwrap();
716+
}
717+
718+
// Resize to the exact same capacity (no free space)
719+
let mut new_memory = [0; CAPACITY * node_size::<usize>()];
720+
storage.resize(&mut new_memory);
721+
722+
// Verify that available is null indicating no free space
723+
assert!(storage.available.get().is_null());
724+
725+
// Verify that no more nodes can be added
726+
assert!(storage.add(99).is_err());
727+
assert_eq!(storage.len(), CAPACITY);
728+
}
729+
703730
#[test]
704731
fn test_swap_works() {
705732
let p1 = Node::new(1);

0 commit comments

Comments
 (0)