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

read_line panics on non-empty buffer #124

Open
nanoqsh opened this issue Feb 3, 2025 · 0 comments · May be fixed by #126
Open

read_line panics on non-empty buffer #124

nanoqsh opened this issue Feb 3, 2025 · 0 comments · May be fixed by #126

Comments

@nanoqsh
Copy link
Contributor

nanoqsh commented Feb 3, 2025

I found some weird behavior of read_line
It panics in debug on non-empty buffer:

use futures_lite::{future, AsyncBufReadExt};

future::block_on(async {
    let mut bytes = "foo".as_bytes();
    let mut buf = String::from("bar");
    _ = bytes.read_line(&mut buf).await;
});

This code prints

thread 'main' panicked at /home/nano/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-lite-2.6.0/src/io.rs:1909:13:
assertion failed: buf.is_empty()
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Although this is a bit of unexpected behavior since read_line from std appends a string.

It is also written in both documentations:

All bytes up to, and including, the newline delimiter (if found) will be appended to buf.

However, nothing is said about you can't use a non-empty string. I think it would be better to repeat std semantic and to append the buffer, at least when it's non empty

notgull added a commit that referenced this issue Feb 16, 2025
The behavior of the canonical read_line is to append to whatever buffer
is passed into it. At the moment it assumes the buffer is empty and
panics otherwise. This commit adds a check for the buffer being empty,
and appends the string otherwise. This is very inefficient but should be
relatively uncommon.

This commit also adds a unit test to prevent regressions.

Fixes #124

Signed-off-by: John Nunley <[email protected]>
@notgull notgull linked a pull request Feb 16, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant