Conversation
`SliceVec::split_off` documents "Panics ... if `at` > `self.len()`", but it split the whole backing slice rather than the initialised part, so an `at` between `len` and `capacity` was accepted. `new.len = self.len - at` then underflowed: a debug build panicked with "attempt to subtract with overflow", and a release build produced a `SliceVec` with `len == usize::MAX`. Add the same length check `ArrayVec::split_off` already performs, plus regression tests. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
SliceVec::split_offdocuments## Panics * if at > self.len(), but it never checksatagainstself.len(). It takes the whole backing slice and splits that, sosplit_at_mutonly rejectsat > capacity. For anyatin(len, capacity],self.len - atunderflows.ArrayVec::split_offandTinyVec::split_offboth do perform the check;SliceVecis the odd one out.Reproduction
On
main(e343bbb, v1.13.3):Debug: it panics, but on the underflow —
attempt to subtract with overflowatslicevec.rs:565, pointing at tinyvec's internals rather than the caller's mistake.Release (
cargo test --release, overflow checks off): no panic at all.The returned
SliceVechaslen == usize::MAXover a 2-element slice, so the next&sv2[..]panics insideDeref. And the receiver silently grew from 2 to 3, handing back an element nobody pushed.The change
The same guard
ArrayVec::split_offalready uses, worded the same way, plus two regression tests (split_off_past_len_panics, andsplit_off_at_len_is_allowedto pin the off-by-one). The doc comment becomes true as written, so it is unchanged.This is a behaviour change and it is yours to call. Code that "worked" in release now panics. I think that is right given what it produced instead, but if you would rather have a debug assertion, or a
try_split_offshape, say which and I will redo it.Verified on macOS arm64, rustc 1.98.1:
cargo testwithalloc,std,grab_spare_slice,latest_stable_rustall green, andcargo test --release --libis the one that matters —split_off_past_len_panicsfails onmainand passes here.cargo fmt --checkclean. I did not run--all-featuresor thedebugger_visualizertest.No
changelog.mdentry — the history suggests those are written at release time, but glad to add one.