From 6cbe8a23afc8744a6228f4d813a690bdaea34939 Mon Sep 17 00:00:00 2001 From: matt rice Date: Fri, 8 Mar 2024 11:28:03 -0800 Subject: [PATCH 1/2] Fix UB when calling `get()` after `clear()` for `InlineStableVec` --- src/core/option.rs | 2 +- src/tests.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/option.rs b/src/core/option.rs index c4e2ef8..73a7daf 100644 --- a/src/core/option.rs +++ b/src/core/option.rs @@ -162,7 +162,7 @@ impl Core for OptionCore { 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) { diff --git a/src/tests.rs b/src/tests.rs index a981070..2f7c21b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -896,6 +896,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] From ad995f70a45def0d8986cd84657f303e217e4257 Mon Sep 17 00:00:00 2001 From: matt rice Date: Fri, 8 Mar 2024 13:35:36 -0800 Subject: [PATCH 2/2] Add another check to the `with_capacity()` tests. --- src/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tests.rs b/src/tests.rs index 2f7c21b..a89bbe5 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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]