From 1735b98380745343ab37a270cebfbcfcb0466c7b Mon Sep 17 00:00:00 2001 From: Sandeep Vijayarao Date: Thu, 5 Nov 2026 17:51:18 -0800 Subject: [PATCH 1/2] [BasicContainers] Fix double destroy in UniqueDictionary.updateValue(forKey:with:) When the updater closure removes an existing entry (sets the value to nil), `UniqueDictionary.updateValue(forKey:with:)` called `_removeValue(at:)`. But the value had already been moved out of its slot before calling the updater, and `_removeValue(at:)` moves it out again and discards the result, destroying uninitialized memory. This leads to crashes or memory corruption. `RigidDictionary.updateValue(forKey:with:)` already handles this case correctly, by deinitializing just the key and resolving the hole. Add `UniqueDictionary._removeEntry(withMovedValueAt:)`, which does the same and then shrinks storage under the same condition as `_removeValue(at:)`, and use it for the removal path. --- .../UniqueDictionary+Insertions.swift | 3 +- .../UniqueDictionary+Removals.swift | 14 ++++++++ .../UniqueDictionaryTests.swift | 33 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift index 5defa8de..bdeb8338 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift @@ -93,7 +93,8 @@ extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { if let value = value.take() { // Simple update unsafe _storage._valuePtr(at: bucket).initialize(to: value) } else { // Removal - _ = unsafe _removeValue(at: bucket) + // The value has already been moved out of its slot above. + unsafe _removeEntry(withMovedValueAt: bucket) } } else if let value = value.take() { // Insertion. diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift index 113398c9..347f2a5d 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift @@ -34,6 +34,20 @@ extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { _resize(minimumCapacity: self.count) return result } + + /// Remove the entry at `bucket`, assuming its value slot has already been + /// moved out (e.g., by `updateValue(forKey:with:)`). Shrinks storage the + /// same way as `_removeValue(at:)`. + @inlinable + @unsafe + package mutating func _removeEntry(withMovedValueAt bucket: _Bucket) { + let shrink = self.count <= _HTable.minimumCapacity(forScale: self._scale) + unsafe _storage._keyPtr(at: bucket).deinitialize(count: 1) + unsafe _storage._resolveHole(at: bucket) + if shrink { + _resize(minimumCapacity: self.count) + } + } @inlinable @inline(__always) diff --git a/Tests/BasicContainersTests/UniqueDictionaryTests.swift b/Tests/BasicContainersTests/UniqueDictionaryTests.swift index f4f4aff4..7ec9a6b9 100644 --- a/Tests/BasicContainersTests/UniqueDictionaryTests.swift +++ b/Tests/BasicContainersTests/UniqueDictionaryTests.swift @@ -364,6 +364,39 @@ class UniqueDictionaryTests: CollectionTestCase { } } + func test_updateValue_closure_removal() { + // Removing an entry by setting the value to nil in + // `updateValue(forKey:with:)` must not destroy the (already moved-out) + // value a second time. Removing everything also exercises shrinking. + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("count", in: [1, 2, 10, 100, 200]) { count in + withLifetimeTracking { tracker in + var d = UniqueDictionary() + for i in 0 ..< count { + d.insertValue(tracker.instance(for: "\(i)"), forKey: tracker.instance(for: i)) + } + withEvery("key", in: 0 ..< count) { key in + let old = d.updateValue(forKey: tracker.instance(for: key)) { value in + let old = value?.payload + value = nil + return old + } + expectEqual(old, "\(key)") + expectEqual(d.count, count - key - 1) + expectTrue(d._checkInvariants()) + for other in key + 1 ..< count { + expectEqual( + d.withValue(forKey: tracker.instance(for: other)) { $0.payload }, + "\(other)") + } + } + expectEqual(d.count, 0) + expectEqual(tracker.instances, 0) + } + } + } + func test_removeAll() { typealias Key = LifetimeTracked typealias Value = LifetimeTracked From dbc1c194de40dfebef6e3f762e9ba809d944305e Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 5 Nov 2026 18:29:11 -0800 Subject: [PATCH 2/2] Add performance FIXME --- .../UniqueDictionary/UniqueDictionary+Removals.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift index 347f2a5d..0d871718 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift @@ -43,6 +43,7 @@ extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { package mutating func _removeEntry(withMovedValueAt bucket: _Bucket) { let shrink = self.count <= _HTable.minimumCapacity(forScale: self._scale) unsafe _storage._keyPtr(at: bucket).deinitialize(count: 1) + // FIXME: Avoid fully resolving the hole when `shrink` is true -- see `_punchHole` unsafe _storage._resolveHole(at: bucket) if shrink { _resize(minimumCapacity: self.count)