Skip to content

Commit bc0602f

Browse files
committed
rcb: ensure correct alignment during transmute
1 parent 480e040 commit bc0602f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/algorithms/recursive_bisection.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use nalgebra::ToTypenum;
1212
use rayon::prelude::*;
1313
use std::cmp;
1414
use std::iter::Sum;
15-
use std::mem;
1615
use std::ops::Add;
1716
use std::ops::AddAssign;
1817
use std::ops::Sub;
@@ -254,9 +253,18 @@ where
254253
});
255254
}
256255

256+
let atomic_partition = unsafe {
257+
// Rust does not seem to have a strict aliasing rule like C does, so the
258+
// transmute here looks safe, but we still need to ensure partition is
259+
// properly aligned for atomic types. While this should always be the
260+
// case, better safe than sorry.
261+
let (before, partition, after) = partition.align_to_mut::<AtomicUsize>();
262+
assert!(before.is_empty() && after.is_empty());
263+
&*partition
264+
};
257265
let mut items: Vec<_> = points
258266
.zip(weights)
259-
.zip(unsafe { mem::transmute::<&mut [usize], &[AtomicUsize]>(&mut *partition) })
267+
.zip(atomic_partition)
260268
.map(|((point, weight), part)| Item {
261269
point,
262270
weight,

0 commit comments

Comments
 (0)