Xuanwo opened a new pull request, #8844:
URL: https://github.com/apache/opendal/pull/8844
# Which issue does this PR close?
N/A
# Rationale for this change
`core/core/src/types/buffer.rs` had two contract violations:
1. `Buf::chunks_vectored` on an empty `Inner::Contiguous` buffer wrote an
empty `IoSlice` into `dst[0]` and returned `1`. `bytes::Buf::chunks_vectored`
requires returning `0` without mutating `dst` when `remaining() == 0`.
Returning an empty slice broke callers such as `Buf::chain(Buffer::new(),
non_empty_buf)` with a 1-slot `dst` and diverged from `Inner::NonContiguous`
(which returned `0`). `Buffer::to_io_slice` had the same inconsistency between
empty `Contiguous` (`vec![IoSlice::new(&[])]`) and exhausted `NonContiguous`
(`vec![]`).
2. `BufferCursor::seek` silently clamped negative and overflowing
`SeekFrom::Current`/`SeekFrom::End` offsets using saturating arithmetic and
clamped `SeekFrom::Start`/`SeekFrom::End` past-end positions to `end`,
contradicting `std::io::Seek` and `std::io::Cursor` (which `BufferCursor`
documents itself as analogous to).
# What changes are included in this PR?
- Delegate `Inner::Contiguous(b)` in `Buf::chunks_vectored` to
`b.chunks_vectored(dst)` so empty contiguous buffers return `0` without
mutating `dst`.
- Return an empty `Vec` from `Buffer::to_io_slice` for empty `Contiguous`
and exhausted `NonContiguous` buffers.
- Update `BufferCursor::seek` to match `std::io::Cursor::seek`:
- `SeekFrom::Start(pos)` sets `pos` directly (allowing seeks past end).
- `SeekFrom::Current(offset)` and `SeekFrom::End(offset)` use
`u64::checked_add_signed`, returning `io::ErrorKind::InvalidInput` without
mutating `self.pos` or `self.view` on negative or overflowing targets.
- Keep `self.view` empty when `self.pos >= self.data.len() as u64` so
reads past end return `Ok(0)` without panicking or truncating `u64` to `usize`.
- Override `Seek::stream_position` to return `Ok(self.pos)` directly, and
update `BufferCursor::read` to advance `self.pos` directly after
`self.view.read`.
- Add unit tests covering empty/exhausted `chunks_vectored` & `to_io_slice`,
`Buf::chain` with a 1-slot `dst`, negative/overflowing `BufferCursor::seek`,
and seeking past end followed by reads and round-trip seeks.
# Are there any user-facing changes?
- `Buffer::chunks_vectored` and `Buffer::to_io_slice` now return 0 slices
for empty contiguous buffers, matching exhausted non-contiguous buffers and the
`bytes::Buf::chunks_vectored` contract.
- `BufferCursor::seek` now matches `std::io::Cursor::seek`: seeking to a
negative or overflowing position returns `io::ErrorKind::InvalidInput` without
mutating cursor position, and seeking past the end preserves the sought
position while subsequent reads return `Ok(0)`.
# Breaking changes
# AI Usage Statement
- Harness: pi / T3 Code
- Model: Gemini 4 Neon
- Effort: High
- Role: Investigated the `Buffer` and `BufferCursor` implementations,
implemented the fixes and deterministic unit tests, and ran formatting, clippy,
unit tests, and doctests on `opendal-core`.
--
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]