Files
Sandeep Vijayarao 1735b98380 [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.
2026-11-05 18:17:48 -08:00

115 lines
3.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.4) && UnstableHashedContainers
@available(SwiftStdlib 5.0, *)
extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable {
@inlinable
@discardableResult
public mutating func insertValue(
_ value: consuming Value,
forKey key: consuming Key
) -> Value? {
var (bucket, hashValue) = _storage._find(key)
if bucket != nil {
return value
}
if _ensureFreeCapacity(1), !_storage._isSmall {
hashValue = _storage._keys._hashValue(for: key)
}
unsafe _storage._insertNew(key, hashValue: hashValue, value)
return nil
}
@inlinable
@discardableResult
public mutating func updateValue(
_ value: consuming Value,
forKey key: consuming Key
) -> Value? {
var (bucket, hashValue) = _storage._find(key)
if let bucket {
return unsafe exchange(&_storage._valuePtr(at: bucket).pointee, with: value)
}
if _ensureFreeCapacity(1), !_storage._isSmall {
hashValue = _storage._keys._hashValue(for: key)
}
unsafe _storage._insertNew(key, hashValue: hashValue, value)
return nil
}
@available(SwiftStdlib 6.4, *)
@inlinable
@discardableResult
@_lifetime(&self)
public mutating func memoizedValue<E: Error>(
forKey key: consuming Key,
_ body: (borrowing Key) throws(E) -> Value
) throws(E) -> Ref<Value> {
let r = _storage._find(key)
let bucket: _Bucket
if let b = r.bucket {
bucket = b
} else {
let value = try body(key)
var hashValue = r.hashValue
if _ensureFreeCapacity(1), !_storage._isSmall {
hashValue = _storage._keys._hashValue(for: key)
}
bucket = unsafe _storage._insertNew(key, hashValue: hashValue, value)
}
return _storage._borrowValue(at: bucket)
}
@inlinable
@discardableResult
public mutating func updateValue<E: Error, R: ~Copyable>(
forKey key: consuming Key,
with updater: (inout Value?) throws(E) -> R
) throws(E) -> R {
var bucket: _Bucket?
var value: Value?
let r = _storage._find(key)
bucket = r.bucket
if let bucket {
value = unsafe _storage._valuePtr(at: bucket).move()
}
var key: Key? = key // To work around inability to consume key in deinit
defer {
if let bucket {
if let value = value.take() { // Simple update
unsafe _storage._valuePtr(at: bucket).initialize(to: value)
} else { // Removal
// The value has already been moved out of its slot above.
unsafe _removeEntry(withMovedValueAt: bucket)
}
} else if let value = value.take() {
// Insertion.
let key = key.take()!
var hashValue = r.hashValue
if _ensureFreeCapacity(1), !_storage._isSmall {
hashValue = _storage._keys._hashValue(for: key)
}
unsafe _storage._insertNew(key, hashValue: hashValue, value)
}
}
return try updater(&value)
}
}
#endif