From 8f2e4bba2027967daa62feeeb528e889bad8d8c3 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 1 May 2026 17:14:21 -0700 Subject: [PATCH] [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.. --- .../BTree/_Node.UnsafeHandle+Insertion.swift | 2 +- .../BTree/BTree.Builder Tests.swift | 37 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Sources/SortedCollections/BTree/_Node.UnsafeHandle+Insertion.swift b/Sources/SortedCollections/BTree/_Node.UnsafeHandle+Insertion.swift index 1f2ef2f7..0f1b7c27 100644 --- a/Sources/SortedCollections/BTree/_Node.UnsafeHandle+Insertion.swift +++ b/Sources/SortedCollections/BTree/_Node.UnsafeHandle+Insertion.swift @@ -498,7 +498,7 @@ extension _Node.UnsafeHandle { ) if !self.isLeaf { - rightHandle.moveInitializeElements( + rightHandle.moveInitializeChildren( count: rightHandle.childCount, fromSlot: 0, toSlot: self.childCount, diff --git a/Tests/SortedCollectionsTests/BTree/BTree.Builder Tests.swift b/Tests/SortedCollectionsTests/BTree/BTree.Builder Tests.swift index d3cb7a53..27938fcc 100644 --- a/Tests/SortedCollectionsTests/BTree/BTree.Builder Tests.swift +++ b/Tests/SortedCollectionsTests/BTree/BTree.Builder Tests.swift @@ -21,16 +21,49 @@ final class BTreeBuilderTests: CollectionTestCase { func test_append() { withEvery("size", in: 0..<1000) { size in var builder = _BTree.Builder(capacity: 4) - + for i in 0..