-
Notifications
You must be signed in to change notification settings - Fork 13
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Currently ErasablePtr
doesn't require that Clone
preserves that the pointer is aligned. So it's possible to construct a type like this
struct FancyBox {
ptr: *mut u32,
}
impl FancyBox {
fn new(value: u32) -> Self {
FancyBox {
ptr: Box::into_raw(Box::new(value))
}
}
}
impl Clone for FancyBox {
fn clone(&self) -> Self {
let mut x = FancyBox::new(**self);
x.ptr = x.ptr.map_addr(|ptr| ptr | 1);
x
}
}
impl core::ops::Deref for FancyBox {
type Target = u32;
fn deref(&self) -> &u32 {
let ptr = self.ptr.map_addr(|ptr| ptr & !3usize);
unsafe { &*ptr }
}
}
fn main() {
let x = FancyBox::new(10);
assert_eq!(*x, 10);
let y = x.clone();
assert_eq!(*y, 10);
}
In particular in affects impl Clone for UnionN
pointer-utils/crates/ptr-union/src/lib.rs
Line 561 in 26c260e
let builder = unsafe { <$Builder<$($A,)*>>::new_unchecked() }; |
since here it's assumed that Clone
will always return an aligned pointer.
So either ErasablePtr
needs to be updated to include this, or UnionN
needs to check that the cloned pointers are aligned.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working