Files
swift-collections/Sources
Karoy LorenteyandClaude Sonnet 4.6 8f2e4bba20 [SortedCollections] _Node.UnsafeHandle.concatenateWith: Fix children buffer corruption in simple-merge path
When two same-depth internal nodes plus a separator fit in a single
node, `concatenateWith` takes the simple-merge branch.  The non-leaf
subbranch was calling `moveInitializeElements` to move children, but
that method operates on the keys/values buffers, not the children
buffer.  The consequences:

- The re-read from `rightHandle.keys[0..<childCount]` hits slots that
  were already moved-out by the preceding `moveInitializeElements`
  call, which is UB.
- Writes land on top of keys just written by that same prior call,
  corrupting them.
- `self.children` is left with its original (too few) children plus
  uninitialized slots; subsequent traversal dereferences garbage.
- `rightHandle.drop()` deallocates the children buffer without
  transferring ownership, leaking the `_Node.Storage` references.

Fix: call `moveInitializeChildren` instead, matching the splinter
branch just above.

Add `test_join_nonLeafSimpleMerge`, which builds two depth-1 internal
nodes whose combined element count fits in capacity 4 and joins them
via `_Node.join`.  Without the fix the resulting tree's children are
undefined, so iteration either crashes or yields garbage.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-01 17:14:21 -07:00
..
2026-04-30 13:35:01 -07:00