Xuanwo opened a new pull request, #8736:
URL: https://github.com/apache/opendal/pull/8736

   # Which issue does this PR close?
   
   None. The bug was found during maintenance review of `Buffer` internals.
   
   # Rationale for this change
   
   Non-contiguous `Buffer` constructors (`From<Vec<Bytes>>`, 
`From<VecDeque<Bytes>>`, `From<Arc<[Bytes]>>`, `FromIterator<Bytes>`) kept 
empty `Bytes` parts. A leading (or only) empty part then made `Buf::chunk()` 
return `&[]` while `remaining() > 0`, violating the `bytes::Buf` contract that 
`chunk()` is never empty while bytes remain. Concretely:
   
   - `to_vec()`, `to_bytes()`, and `Buf::copy_to_slice()` spun forever calling 
`chunk()` + `advance(0)`.
   - `std::io::Read::read()` and `BufRead::fill_buf()` returned `Ok(0)` / 
`Ok(&[])` prematurely, silently truncating the buffer for any `Read` consumer.
   - `Buffer::current()` indexed `parts[idx]` unconditionally, panicking with 
index-out-of-bounds on `Buffer::from(Vec::<Bytes>::new()).current()` and after 
`advance(len)` moved `idx` past the end.
   
   Two related issues found while auditing: `Buffer::count()` reported `1` for 
an exhausted view with a non-zero `offset` (e.g. after `truncate(0)`), and 
`Iterator::size_hint`'s lower bound (`parts.len() - idx`) overstated the 
remaining items after `truncate()`, which violates the `Iterator` contract.
   
   # What changes are included in this PR?
   
   - All multi-part constructors drop empty `Bytes`; views backed by zero or 
one non-empty part collapse to `Inner::Contiguous`, so `NonContiguous` always 
holds at least two non-empty parts (documented as a type invariant) and 
single-part buffers no longer pay for the `Arc` allocation.
   - `current()` returns `Bytes::new()` when the view is empty or `idx` has 
advanced past the end, matching `Contiguous` behavior.
   - `count()` returns `0` for an exhausted view; `Iterator::size_hint` reports 
the exact remaining item count.
   - `to_bytes()` stays zero-copy when the whole view lives inside a single 
part (e.g. after `slice`/`split_to`), generalizing the previous `parts.len() == 
1` special case.
   
   # Are there any user-facing changes?
   
   Bug fixes only; no API changes. `Buffer`s built from parts containing empty 
`Bytes` now behave correctly instead of hanging, truncating reads, or panicking.
   
   # Breaking changes
   
   # AI Usage Statement
   
   - Harness: pi coding agent (delegated sub-agent via T3 Code)
   - Model: devin/swe-2
   - Effort: max
   - Role: AI authored the fix and tests from a verified bug report, ran the 
listed test/lint/fmt commands locally, and prepared this PR; a human maintainer 
remains accountable for review and merge.
   


-- 
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]

Reply via email to