Skip to content

Commit

Permalink
Merge pull request #45 from ratmice/clear_get_ub
Browse files Browse the repository at this point in the history
Fix UB when calling `get()` after `clear()` for `InlineStableVec`
  • Loading branch information
LukasKalbertodt authored Mar 10, 2024
2 parents 7e6d29c + ad995f7 commit 1e48278
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl<T> Core<T> for OptionCore<T> {
unsafe fn has_element_at(&self, idx: usize) -> bool {
debug_assert!(idx < self.cap());

self.data.get_unchecked(idx).is_some()
idx < self.len() && self.data.get_unchecked(idx).is_some()
}

unsafe fn insert_at(&mut self, idx: usize, elem: T) {
Expand Down
2 changes: 2 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ macro_rules! gen_tests_for {

assert!(sv.capacity() >= 3);
assert_sv_eq!(sv, []: String);
assert_eq!(sv.get(0), None);
}

#[test]
Expand Down Expand Up @@ -896,6 +897,7 @@ macro_rules! gen_tests_for {
let mut sv = $ty::from_iter(vec![1, 3, 5]);
sv.clear();
assert_sv_eq!(sv, []: u32);
assert_eq!(sv.get(0), None);
}

#[test]
Expand Down

0 comments on commit 1e48278

Please sign in to comment.