Skip to content
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

Does TokioIo::poll_read violate the safety requirement of ReadBufCursor::as_mut? #3819

Closed
zacknewman opened this issue Dec 28, 2024 · 1 comment
Labels
C-bug Category: bug. Something is wrong. This is bad!

Comments

@zacknewman
Copy link

zacknewman commented Dec 28, 2024

I'm unable to create an issue or start a discussion in hyper-util, so I'm starting one here. I have essentially zero experience with unsafe code except in the simplest cases, so I apologize if this question is rudimentary.

Anyway, the safety comment for ReadBufCursor::as_mut states "The caller must not uninitialize any bytes that may have been initialized before.". TokioIo implements Read::poll_read in a way that appears to violate this requirement. Specifically, when AsyncRead::poll_read errs it immediately returns the error without advancing the buffer. Doesn't this mean that a call to Read::poll_read after an error will cause previously initialized memory to be unitialized when the previous call initialized some of the bytes before erring?

impl<T> hyper::rt::Read for TokioIo<T>
where
    T: tokio::io::AsyncRead,
{
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        mut buf: hyper::rt::ReadBufCursor<'_>,
    ) -> Poll<Result<(), std::io::Error>> {
        let n = unsafe {
            let mut tbuf = tokio::io::ReadBuf::uninit(buf.as_mut());
            match tokio::io::AsyncRead::poll_read(self.project().inner, cx, &mut tbuf) {
                Poll::Ready(Ok(())) => tbuf.filled().len(),
                // When `other` is `Poll::Ready(Err(_))`, isn't it possible that the above call to
                // `AsyncRead::poll_read` initialized at least some of `tbuf` meaning calling
                // `tokio::io::ReadBuf::uninit(buf.as_mut())` again violates the safety requirement?
                other => return other,
            }
        };

        unsafe {
            buf.advance(n);
        }
        Poll::Ready(Ok(()))
    }
}
@zacknewman zacknewman added the C-bug Category: bug. Something is wrong. This is bad! label Dec 28, 2024
@zacknewman
Copy link
Author

Not an issue. Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug. Something is wrong. This is bad!
Projects
None yet
Development

No branches or pull requests

1 participant