mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
udp: document and shrink some unsafe blocks (#4655)
This documents why it is safe to convert `bytes::UninitSlice` to `&mut [MaybeUninit<u8>]`, and shrinks one of the unsafe blocks to make these functions easier to audit.
This commit is contained in:
parent
2a305d2423
commit
c5ff797dcf
@ -115,6 +115,9 @@ mod util {
|
|||||||
|
|
||||||
let n = {
|
let n = {
|
||||||
let dst = buf.chunk_mut();
|
let dst = buf.chunk_mut();
|
||||||
|
|
||||||
|
// Safety: `chunk_mut()` returns a `&mut UninitSlice`, and `UninitSlice` is a
|
||||||
|
// transparent wrapper around `[MaybeUninit<u8>]`.
|
||||||
let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
|
let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
|
||||||
let mut buf = ReadBuf::uninit(dst);
|
let mut buf = ReadBuf::uninit(dst);
|
||||||
let ptr = buf.filled().as_ptr();
|
let ptr = buf.filled().as_ptr();
|
||||||
|
@ -81,17 +81,21 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We're out of data. Try and fetch more data to decode
|
// We're out of data. Try and fetch more data to decode
|
||||||
let addr = unsafe {
|
let addr = {
|
||||||
// Convert `&mut [MaybeUnit<u8>]` to `&mut [u8]` because we will be
|
// Safety: `chunk_mut()` returns a `&mut UninitSlice`, and `UninitSlice` is a
|
||||||
// writing to it via `poll_recv_from` and therefore initializing the memory.
|
// transparent wrapper around `[MaybeUninit<u8>]`.
|
||||||
let buf = &mut *(pin.rd.chunk_mut() as *mut _ as *mut [MaybeUninit<u8>]);
|
let buf = unsafe { &mut *(pin.rd.chunk_mut() as *mut _ as *mut [MaybeUninit<u8>]) };
|
||||||
let mut read = ReadBuf::uninit(buf);
|
let mut read = ReadBuf::uninit(buf);
|
||||||
let ptr = read.filled().as_ptr();
|
let ptr = read.filled().as_ptr();
|
||||||
let res = ready!(pin.socket.borrow().poll_recv_from(cx, &mut read));
|
let res = ready!(pin.socket.borrow().poll_recv_from(cx, &mut read));
|
||||||
|
|
||||||
assert_eq!(ptr, read.filled().as_ptr());
|
assert_eq!(ptr, read.filled().as_ptr());
|
||||||
let addr = res?;
|
let addr = res?;
|
||||||
pin.rd.advance_mut(read.filled().len());
|
|
||||||
|
// Safety: This is guaranteed to be the number of initialized (and read) bytes due
|
||||||
|
// to the invariants provided by `ReadBuf::filled`.
|
||||||
|
unsafe { pin.rd.advance_mut(read.filled().len()) };
|
||||||
|
|
||||||
addr
|
addr
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user