Skip to content

Commit 7a8ee3e

Browse files
authored
consolidation: fix UB from mutable pointer aliasing (#353)
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 <petrosagg@gmail.com>
1 parent 19808dc commit 7a8ee3e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/consolidation.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn consolidate_slice<T: Ord, R: Semigroup>(slice: &mut [(T, R)]) -> usize {
6666
offset += 1;
6767
}
6868
let ptr1 = slice.as_mut_ptr().offset(offset as isize);
69-
std::mem::swap(&mut *ptr1, &mut *ptr2);
69+
std::ptr::swap(ptr1, ptr2);
7070
}
7171
}
7272
}
@@ -129,7 +129,7 @@ pub fn consolidate_updates_slice<D: Ord, T: Ord, R: Semigroup>(slice: &mut [(D,
129129
offset += 1;
130130
}
131131
let ptr1 = slice.as_mut_ptr().offset(offset as isize);
132-
std::mem::swap(&mut *ptr1, &mut *ptr2);
132+
std::ptr::swap(ptr1, ptr2);
133133
}
134134

135135
}
@@ -164,6 +164,10 @@ mod tests {
164164
vec![("a", 0), ("b", 0)],
165165
vec![],
166166
),
167+
(
168+
vec![("a", 1), ("b", 1)],
169+
vec![("a", 1), ("b", 1)],
170+
),
167171
];
168172

169173
for (mut input, output) in test_cases {
@@ -192,6 +196,10 @@ mod tests {
192196
vec![("a", 1, 0), ("b", 1, 0)],
193197
vec![],
194198
),
199+
(
200+
vec![("a", 1, 1), ("b", 2, 1)],
201+
vec![("a", 1, 1), ("b", 2, 1)],
202+
),
195203
];
196204

197205
for (mut input, output) in test_cases {

0 commit comments

Comments
 (0)