[SortedCollections] Balance the child after remove(at:)

remove(at:) recursed into the child holding the offset and returned
without balancing it, so repeated removals could empty a leaf. Iterating
then read a stale slot, which returned already-removed elements and read
released references in release builds, and trapped in debug builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Swastik TripathiandClaude Opus 5.5 committed 2026-11-01 09:43:22 +05:30
1 parent f96b909e1a
commit 93745231f1
3 files changed
+55

No files matched your search

@@ -117,6 +117,10 @@ extension _Node.UnsafeHandle {
// Reduce the subtree count.
unsafe self.subtreeCount -= 1
// Balance the child slot, as the removal may have brought it out of
// balance.
unsafe self.balance(atSlot: childSlot)
return element
} else if offset == endIndex {
let predecessor = unsafe self[childAt: childSlot].update {
@@ -240,6 +240,33 @@ final class SortedDictionaryTests: CollectionTestCase {
}
}
func test_repeatedRemoveAtIndex() {
// Removing by index has to keep rebalancing the tree, or a leaf eventually
// runs out of elements. (https://github.com/apple/swift-collections/issues/809)
func pickFirst(_ count: Int) -> Int { 0 }
func pickLast(_ count: Int) -> Int { count - 1 }
func pickMiddle(_ count: Int) -> Int { count / 2 }
withEvery("count", in: 0 ..< 60) { count in
withEvery("pick", in: [pickFirst, pickLast, pickMiddle]) { pick in
var sorted = SortedDictionary(keysWithValues: (0 ..< count).map { ($0, $0 * 2) })
var comparisonKeys = Array(0 ..< count)
while !comparisonKeys.isEmpty {
let offset = pick(comparisonKeys.count)
let removed = sorted.remove(at: sorted.index(sorted.startIndex, offsetBy: offset))
let key = comparisonKeys.remove(at: offset)
expectEqual(removed.key, key)
expectEqual(removed.value, key * 2)
expectEqual(sorted.count, comparisonKeys.count)
expectEqualElements(sorted.map { $0.key }, comparisonKeys)
expectEqualElements(sorted.map { $0.value }, comparisonKeys.map { $0 * 2 })
}
}
}
}
func test_modifySubscriptRemoval() {
func modify(_ value: inout Int?, setTo newValue: Int?) {
value = newValue
@@ -188,6 +188,30 @@ class SortedSetTests: CollectionTestCase {
}
}
func test_repeatedRemoveAtIndex() {
// Removing by index has to keep rebalancing the tree, or a leaf eventually
// runs out of elements. (https://github.com/apple/swift-collections/issues/809)
func pickFirst(_ count: Int) -> Int { 0 }
func pickLast(_ count: Int) -> Int { count - 1 }
func pickMiddle(_ count: Int) -> Int { count / 2 }
withEvery("count", in: 0 ..< 60) { count in
withEvery("pick", in: [pickFirst, pickLast, pickMiddle]) { pick in
var sorted = SortedSet(0 ..< count)
var comparisonKeys = Array(0 ..< count)
while !comparisonKeys.isEmpty {
let offset = pick(comparisonKeys.count)
let removed = sorted.remove(at: sorted.index(sorted.startIndex, offsetBy: offset))
expectEqual(removed, comparisonKeys.remove(at: offset))
expectEqual(sorted.count, comparisonKeys.count)
expectEqualElements(sorted, comparisonKeys)
}
}
}
}
func test_CustomStringConvertible() {
let a: SortedSet<Int> = []
expectEqual(a.description, "[]")