-
Notifications
You must be signed in to change notification settings - Fork 848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(api): Check if pointer is aligned before reading #5449
base: main
Are you sure you want to change the base?
Conversation
✅ No documentation updates required. |
let buf_ptr = out.as_mut_ptr() as *mut u8; | ||
if !buf_ptr.is_aligned() { | ||
return Err(MemoryAccessError::UnalignedPointer); | ||
} | ||
let buf = unsafe { slice::from_raw_parts_mut(buf_ptr, mem::size_of::<T>()) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't .is_aligned()
always going to return true due to this?
MaybeUninit is guaranteed to have the same size, alignment, and ABI as T:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends... These guarantees only hold as long as we don't have unsafe code, and we do have a lot of unsafe code near the syscall boundaries. Still, @xdoardo would know better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But also, since we're casting to *mut u8
... Let's add a test?
let buf_ptr = out.as_mut_ptr() as *mut u8; | ||
if !buf_ptr.is_aligned() { | ||
return Err(MemoryAccessError::UnalignedPointer); | ||
} | ||
let buf = unsafe { slice::from_raw_parts_mut(buf_ptr, mem::size_of::<T>()) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But also, since we're casting to *mut u8
... Let's add a test?
As per title. Fixes #5444