mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
The `_trim(first:)` and `_trim(last:)` helpers reassign `self` to the
remaining slice and then build the trimmed result from `self`'s now-updated
`baseAddress`/`count`, so they hand back a region that points into the wrong
place in the buffer.
`UnsafeMutableBufferPointer._trim(first:)` had this exact bug fixed earlier (it
captures `oldStart` before the reassignment), but three sibling helpers were
left untouched:
* `UnsafeBufferPointer._trim(first:)` reads `baseAddress` after advancing
`self`, so the returned prefix starts at the remainder instead of at the
original head.
* `UnsafeBufferPointer._trim(last:)` and
`UnsafeMutableBufferPointer._trim(last:)` recompute `count &- cut` after
shrinking `self`, so the returned suffix starts `cut` elements too early.
For a five-element buffer, `_trim(first: 2)` returned the bytes of `[30, 40]`
rather than `[10, 20]`, and `_trim(last: 2)` returned `[20, 30]` rather than
`[40, 50]`; trimming more than the count walked off the end of the buffer.
Capture the original start (and, for the suffix case, the post-trim count)
before mutating `self`, matching the already-correct `_trim(first:)` on the
mutable buffer. Adds direct tests covering both overloads on both buffer types.