Skip to content

Commit

Permalink
io: fix unsound pin projection in read_buf and write_buf (#2612)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored Jun 12, 2020
1 parent 1636910 commit 1769f65
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tokio/src/io/util/async_read_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ cfg_io_util! {
/// ```
fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
where
Self: Sized,
Self: Sized + Unpin,
B: BufMut,
{
read_buf(self, buf)
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/io/util/async_write_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ cfg_io_util! {
/// ```
fn write_buf<'a, B>(&'a mut self, src: &'a mut B) -> WriteBuf<'a, Self, B>
where
Self: Sized,
Self: Sized + Unpin,
B: Buf,
{
write_buf(self, src)
Expand Down
13 changes: 5 additions & 8 deletions tokio/src/io/util/read_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::task::{Context, Poll};

pub(crate) fn read_buf<'a, R, B>(reader: &'a mut R, buf: &'a mut B) -> ReadBuf<'a, R, B>
where
R: AsyncRead,
R: AsyncRead + Unpin,
B: BufMut,
{
ReadBuf { reader, buf }
Expand All @@ -26,16 +26,13 @@ cfg_io_util! {

impl<R, B> Future for ReadBuf<'_, R, B>
where
R: AsyncRead,
R: AsyncRead + Unpin,
B: BufMut,
{
type Output = io::Result<usize>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
// safety: no data is moved from self
unsafe {
let me = self.get_unchecked_mut();
Pin::new_unchecked(&mut *me.reader).poll_read_buf(cx, &mut me.buf)
}
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
let me = &mut *self;
Pin::new(&mut *me.reader).poll_read_buf(cx, me.buf)
}
}
13 changes: 5 additions & 8 deletions tokio/src/io/util/write_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,21 @@ cfg_io_util! {
/// asynchronous manner, returning a future.
pub(crate) fn write_buf<'a, W, B>(writer: &'a mut W, buf: &'a mut B) -> WriteBuf<'a, W, B>
where
W: AsyncWrite,
W: AsyncWrite + Unpin,
B: Buf,
{
WriteBuf { writer, buf }
}

impl<W, B> Future for WriteBuf<'_, W, B>
where
W: AsyncWrite,
W: AsyncWrite + Unpin,
B: Buf,
{
type Output = io::Result<usize>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
// safety: no data is moved from self
unsafe {
let me = self.get_unchecked_mut();
Pin::new_unchecked(&mut *me.writer).poll_write_buf(cx, &mut me.buf)
}
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
let me = &mut *self;
Pin::new(&mut *me.writer).poll_write_buf(cx, me.buf)
}
}

0 comments on commit 1769f65

Please sign in to comment.