From 7b7c60a369c743170ca6f496ee44ad0750673281 Mon Sep 17 00:00:00 2001 From: Petros Angelatos Date: Sun, 20 Feb 2022 12:52:13 +0100 Subject: [PATCH] consolidation: fix UB from mutable pointer aliasing Running the added testcase under miri without the equality check results in Undefined Behaviour from creating two mutable references pointing to the same location. Signed-off-by: Petros Angelatos --- src/consolidation.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/consolidation.rs b/src/consolidation.rs index 379d25256..69b5f4570 100644 --- a/src/consolidation.rs +++ b/src/consolidation.rs @@ -66,7 +66,7 @@ pub fn consolidate_slice(slice: &mut [(T, R)]) -> usize { offset += 1; } let ptr1 = slice.as_mut_ptr().offset(offset as isize); - std::mem::swap(&mut *ptr1, &mut *ptr2); + std::ptr::swap(ptr1, ptr2); } } } @@ -129,7 +129,7 @@ pub fn consolidate_updates_slice(slice: &mut [(D, offset += 1; } let ptr1 = slice.as_mut_ptr().offset(offset as isize); - std::mem::swap(&mut *ptr1, &mut *ptr2); + std::ptr::swap(ptr1, ptr2); } } @@ -164,6 +164,10 @@ mod tests { vec![("a", 0), ("b", 0)], vec![], ), + ( + vec![("a", 1), ("b", 1)], + vec![("a", 1), ("b", 1)], + ), ]; for (mut input, output) in test_cases { @@ -192,6 +196,10 @@ mod tests { vec![("a", 1, 0), ("b", 1, 0)], vec![], ), + ( + vec![("a", 1, 1), ("b", 2, 1)], + vec![("a", 1, 1), ("b", 2, 1)], + ), ]; for (mut input, output) in test_cases {