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

fix: use write_all() to write space after a tag #118

Merged
merged 1 commit into from
Mar 14, 2025

Conversation

link2xt
Copy link
Contributor

@link2xt link2xt commented Mar 14, 2025

write() should not return Ok without writing anything, but the documentation is not clear on this
and underlying implementation may be wrong
as IMAP can be run over various streams,
e.g. compressed, encrypted, proxied.
It is safer to use write_all() everywhere,
even when we write a single byte.

`write()` should not return Ok without writing anything,
but the documentation is not clear on this
and underlying implementation may be wrong
as IMAP can be run over various streams,
e.g. compressed, encrypted, proxied.
It is safer to use `write_all()` everywhere,
even when we write a single byte.
@link2xt link2xt requested a review from Hocuri March 14, 2025 04:42
Copy link
Contributor

@Hocuri Hocuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tokio implementation for write_all() even returns Ready(Err(...)) if 0 bytes were written even though there is still data in the buffer:

impl<W> Future for WriteAll<'_, W>
where
    W: AsyncWrite + Unpin + ?Sized,
{
    type Output = io::Result<()>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        let me = self.project();
        while !me.buf.is_empty() {
            let n = ready!(Pin::new(&mut *me.writer).poll_write(cx, me.buf))?;
            {
                let (_, rest) = mem::take(&mut *me.buf).split_at(n);
                *me.buf = rest;
            }
            if n == 0 {
                return Poll::Ready(Err(io::ErrorKind::WriteZero.into())); // <-- Here
            }
        }

        Poll::Ready(Ok(()))
    }
}

so, if the stream implementation is buggy and returns 0, then the change here won't help anything.

I'll nevertheless approve, since nothing speaks against using write_all here, and it may prevent errors in the future where someone tries to write more than one byte using write().

@link2xt
Copy link
Contributor Author

link2xt commented Mar 14, 2025

There is also async std still supported next to tokio.

@link2xt link2xt merged commit e5b3351 into main Mar 14, 2025
22 checks passed
@link2xt link2xt deleted the link2xt/write-all-one-byte branch March 14, 2025 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants