-
Notifications
You must be signed in to change notification settings - Fork 33
Maybe unsound in WeakPointer::new #177
Copy link
Copy link
Open
Description
Hello, thank you for your contribution in this project, I am scanning the unsoundness problem in rust project.
I notice the following code:
pub struct WeakPointer<T: ?Sized> {
ptr: *mut T,
}
impl<T> WeakPointer<T> {
pub fn new(ptr: *mut T) -> Self {
WeakPointer { ptr }
}
}
impl<T> Deref for WeakPointer<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &(*self.ptr) }
}
}
impl<T> DerefMut for WeakPointer<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut (*self.ptr) }
}
}
Considering that new is also a pub function. I assume that users can directly call this function. This potential situation could result in *self.ptr being dereference a null pointer, and directly dereferencing it might trigger undefined behavior (UB). For safety reasons, I felt it necessary to report this issue. If you have performed checks elsewhere that ensure this is safe, please don’t take offense at my raising this issue.
I suggest Several possible fixes:
- If there is no external usage for
WeakPointerornew, they should not marked aspub, at least itsnewshould not marked aspub newmethod should add additional check for null pointer.- mark new method as unsafe and proper doc to let users know that they should provide valid Pointers.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels