fix(core): Respect logical length in Buffer vectored views - #8064
Open
codeAnqiang-ma wants to merge 1 commit into
Open
fix(core): Respect logical length in Buffer vectored views#8064codeAnqiang-ma wants to merge 1 commit into
codeAnqiang-ma wants to merge 1 commit into
Conversation
`Buffer::to_io_slice` and the `bytes::Buf::chunks_vectored` impl walked every underlying part to its physical end and ignored the logical `size` field. Because `truncate`, `slice`, `split_to` and `split_off` shrink `size` without dropping `parts`, both methods returned more bytes than `remaining()` for a shortened non-contiguous buffer. That violates the documented `chunks_vectored` contract and contradicts `chunk()` and `Iterator::next`, which clamp with `.min(size)`. Clamp both methods to the remaining logical length, mirroring `chunk()`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #8063.
Rationale for this change
For a non-contiguous
Buffer,to_io_slice()and thebytes::Buf::chunks_vectored()impl destructureInner::NonContiguouswith.., dropping the logicalsizefield, and then walk each part to its physical end. Becausetruncate/slice/split_to/split_offshrinksizewithout droppingparts, both methods then hand back more bytes thanremaining()— for a 2-byte view of["abc", "def"]they expose all 6 bytes.That violates the documented
bytes::Buf::chunks_vectoredcontract ("the sum of the lengths of all the buffers written todstwill be less than or equal toBuf::remaining()"), and it contradictschunk()andIterator::nextin the same type, which both clamp with.min(*size)to honor theInner::NonContiguousinvariant ("the logic view … spanssizebytes").#4481 already fixed the
offsethalf of this problem in these same two methods; thesizeclamp was not added then.What changes are included in this PR?
Both non-contiguous branches now bind
size, track it as a runningremaining, clamp each slice with.min(remaining), and stop once it reaches zero — mirroring whatchunk()already does. Nothing else changes.Regression tests
test_vectored_views_after_slice,test_vectored_views_after_split_toandtest_vectored_views_after_split_offassert that both vectored views yield exactly the logical content. They fail onmainand pass here.Test evidence
Before the fix (tests applied to unmodified
main), all three fail — the vectored views expose the whole physical buffer:After the fix:
--features "tokio/time"is only needed to compileopendal-core's#[cfg(test)]code when the package is tested in isolation; it is unrelated to this change. The full-workspace--all-featuresclippy/nextest gate and the behavior tests were not run locally.Are there any user-facing changes?
Yes — a bug fix; no signature or API change. A shortened non-contiguous
Bufferpassed to a vectored write now yields only its logical bytes instead of also emitting the truncated tail.To be precise about the blast radius: there is no active data corruption in-tree. The only in-tree vectored write backend,
compfs, takes its bytes viaBuffer::by_ref().collect()— theIteratorimpl, which already respectssize— so it never reached this path. The defect matters at the public API and trait-contract level:to_io_sliceispuband documented for vectored writes, andchunks_vectoredis abytes::Bufimpl, so downstream users and any future in-tree consumer doing a vectored write over a shortened view are affected.AI Usage Statement
Prepared with AI assistance: Cursor, using Anthropic Claude Opus 5. The AI drafted the patch, the regression tests and this description; I reproduced the defect locally, verified the tests fail before the change and pass after it, and reviewed every line of the diff.