0z5a opened a new issue, #8519:
URL: https://github.com/apache/opendal/issues/8519
### OpenDAL version or commit
main at commit 68de94d. `core/core/src/types/buffer.rs` is identical in
v0.59.4 and v0.59.5, so both releases are affected (`BufferCursor` first
shipped in v0.59.4 via #8320).
### Affected service or component
core: `opendal::BufferCursor` (the `BufRead` impl in
`core/core/src/types/buffer.rs`). No service is involved.
### Describe the bug
`BufferCursor` implements `BufRead`, but `BufRead::consume` only bumps the
internal `pos` field. It does not advance `view`, which is the slice that
`fill_buf` and `read` actually serve bytes from. After `fill_buf()` +
`consume(n)`, the next `fill_buf()` returns the same bytes again.
Every std helper built on `fill_buf`/`consume` is affected:
- `lines()` yields the first line forever.
- `read_line()` returns the first line on each call.
- `read_line()` / `read_until()` on input without the delimiter never reach
EOF and keep appending the same bytes.
- A `read()` after `consume()` returns bytes from the old position and then
jumps past them.
The plain `Read` + `Seek` path (`read_to_end`, `seek`) works, which is
probably why the existing fuzz test does not catch this.
### Steps to Reproduce
macOS 26.6 arm64, rustc 1.100.0, `opendal` from main as a path dependency
with `default-features = false`, plus `bytes = "1.10"`.
```rust
use std::io::{BufRead, Cursor, Read};
use bytes::Bytes;
use opendal::{Buffer, BufferCursor};
fn main() {
let data = || Buffer::from(vec![Bytes::from("line one\nline "),
Bytes::from("two\n")]);
let raw = b"line one\nline two\n".to_vec();
// fill_buf() after consume(5)
let mut cur = BufferCursor::new(data());
cur.fill_buf().unwrap();
cur.consume(5);
println!("BufferCursor fill_buf after consume(5): {:?}",
String::from_utf8_lossy(cur.fill_buf().unwrap()));
let mut b = data();
b.fill_buf().unwrap();
b.consume(5);
println!("Buffer fill_buf after consume(5): {:?}",
String::from_utf8_lossy(b.fill_buf().unwrap()));
// read() after consume(5)
let mut out = [0u8; 3];
let n = cur.read(&mut out).unwrap();
println!("BufferCursor read(3) after consume(5): {:?}",
String::from_utf8_lossy(&out[..n]));
// lines(), bounded with take() because it never reaches EOF
let got: Vec<String> =
BufferCursor::new(data()).lines().take(4).map(Result::unwrap).collect();
let want: Vec<String> =
Cursor::new(raw.clone()).lines().take(4).map(Result::unwrap).collect();
println!("BufferCursor lines().take(4): {got:?}");
println!("std Cursor lines().take(4): {want:?}");
// read_line() on input without a newline, bounded with take(40)
let mut s = String::new();
BufferCursor::new(Buffer::from(Bytes::from("no
newline"))).take(40).read_line(&mut s).unwrap();
println!("BufferCursor take(40).read_line on a 10-byte input: {} bytes
{:?}", s.len(), s);
// control: plain Read path
let mut v = vec![];
BufferCursor::new(data()).read_to_end(&mut v).unwrap();
println!("BufferCursor read_to_end == input: {}", v == raw);
}
```
### Actual Behavior
```
BufferCursor fill_buf after consume(5): "line one\nline "
Buffer fill_buf after consume(5): "one\nline "
BufferCursor read(3) after consume(5): "lin"
BufferCursor lines().take(4): ["line one", "line one", "line one", "line
one"]
std Cursor lines().take(4): ["line one", "line two"]
BufferCursor take(40).read_line on a 10-byte input: 40 bytes "no newlineno
newlineno newlineno newline"
BufferCursor read_to_end == input: true
```
`take()` is only there so the program ends. Without it, `lines()` never
reaches EOF, and `read_line()` on the input without a newline keeps appending.
### Expected Behavior
`BufferCursor` should behave like `std::io::Cursor`, which its docs name as
the model, and like `Buffer`'s own `BufRead` impl:
- `fill_buf()` after `consume(5)` returns `"one\nline "`;
- `read(3)` returns `"one"`;
- `lines()` yields `["line one", "line two"]` and stops;
- `read_line()` returns the 10 input bytes once.
This is the `BufRead` contract: once bytes are passed to `consume`, later
`fill_buf`/`read` calls must not return them again.
### Additional Context
Root cause: `impl BufRead for BufferCursor` in
`core/core/src/types/buffer.rs`. `consume` updates `self.pos` but leaves
`self.view` untouched, while `fill_buf` returns `self.view.fill_buf()`.
`Read::read` reads from `view` and then re-slices `view` from `pos`, so after a
`consume` the two drift apart.
Proposed fix: in `consume`, clamp the amount to `view.remaining()`, advance
`view` by that amount, and add the same amount to `pos`, so the two stay in
sync. I have this locally with a unit test next to
`fuzz_buffer_cursor_seek_and_read`. The test fails on main and passes with the
change.
### Reproduction confirmation
- [x] I reproduced this issue against the affected component. For a
service-specific report, I used the actual service rather than only a mock,
emulator, or source-code analysis.
### Are you willing to submit a PR to fix this bug?
- [x] Yes, I would like to submit a PR.
This issue was drafted with Claude Code (claude-opus-5-5); the results above
are from the runs described.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]