chris-morgan / anymap

A safe and convenient store for one value of each type
Other
326 stars 42 forks source link

Unsound usages of unsafe implementation from `Self` to `T` #51

Open llooFlashooll opened 3 months ago

llooFlashooll commented 3 months ago

Hi, I am scanning the anymap in the latest version with my own static analyzer tool.

Unsafe conversion found at: src/any.rs

#[inline]
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
    &*(self as *const Self as *const T)
}

#[inline]
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
    &mut *(self as *mut Self as *mut T)
}

#[inline]
unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T> {
    Box::from_raw(Box::into_raw(self) as *mut T)
}

This unsound implementation would create a misalignment issues, if the generics type T is not properly handled like it's other random types.

This would potentially cause undefined behaviors in Rust. If we further manipulate the problematic converted types, it would potentially lead to different consequences. I am reporting this issue for your attention.