Skip to content

fixed VecDeque::splice() not filling the buffer correctly when resizing the buffer on start = end range#152258

Open
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:vecdeque_splice_151758
Open

fixed VecDeque::splice() not filling the buffer correctly when resizing the buffer on start = end range#152258
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:vecdeque_splice_151758

Conversation

@asder8215
Copy link
Contributor

@asder8215 asder8215 commented Feb 6, 2026

This PR fixes #151758. The issue came from Splice::move_tail, which as joboet pointed out:

The issue is in move_tail, which resizes the buffer, but fails to account for the resulting hole.

The problem with reserving more space through VecDeque's buf.reserve() is that it doesn't update VecDeque's head, which means that this code in move_tail:

deque.wrap_copy(
    deque.to_physical_idx(tail_start),
    deque.to_physical_idx(new_tail_start),
    self.tail_len,
);

could move over the section of data that tail_start..tail_start + self.tail_len of the buffer is supposed to be held at to the incorrect destination since all .to_physical_idx() is doing is a wrapping add on the VecDeque's head with the passed in idx value.

To avoid this I decided to use VecDeque::reserve to both allocate more space into the VecDeque if necessary and update head appropriately. However, VecDeque::reserve internally relies on the VecDeque's len field. Earlier in VecDeque::splice, it modifies the VecDeque's len constructing the drain via Drain::new (as it does a mem::replace on deque.len with the start bound of the passed in range). The VecDeque's len can also be potentially modified in the earlier Splice::fill() call if it does any replacement towards elements within the passed in range value for VecDeque::splice(). I needed to temporarily restore the VecDeque's len to its actual len, so that VecDeque::reserve can work properly. Afterward, you can bring back the VecDeque's len to what its value was before and fill the gap appropriately with the rest of the replace_with content.

r? @joboet

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 6, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 6, 2026

joboet is currently at their maximum review capacity.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the vecdeque_splice_151758 branch from d33db94 to 2ecddd2 Compare February 6, 2026 22:32
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on a patch that is more clever about copying the elements, but this is certainly a good fix for the meantime. There's an issue though...

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 7, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@asder8215 asder8215 force-pushed the vecdeque_splice_151758 branch from 65cec58 to 748d3c1 Compare February 7, 2026 15:50
@asder8215
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 7, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 12, 2026
@asder8215 asder8215 requested a review from joboet February 13, 2026 16:35
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 13, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 21, 2026
@asder8215 asder8215 requested a review from joboet February 22, 2026 02:08
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 22, 2026
@asder8215 asder8215 force-pushed the vecdeque_splice_151758 branch from 440a328 to 409bb2f Compare February 23, 2026 05:56
@joboet
Copy link
Member

joboet commented Feb 23, 2026

Looks great, thank you! Could you squash your commits, please? I'll approve after that.

@asder8215 asder8215 force-pushed the vecdeque_splice_151758 branch from 409bb2f to bb67ee2 Compare February 23, 2026 16:07
@asder8215
Copy link
Contributor Author

@joboet put the change into one commit. Lmk if there's anything else I need to do!

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 28, 2026
@asder8215 asder8215 force-pushed the vecdeque_splice_151758 branch from bb67ee2 to d0a33ab Compare March 1, 2026 03:29
@asder8215 asder8215 requested a review from joboet March 1, 2026 03:30
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 1, 2026
@asder8215
Copy link
Contributor Author

Hope the new comment/rationale on the change is okay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new VecDeque::splice() method in nightly produce incorrect result

4 participants