Skip to content

Commit fef19d4

Browse files
author
Lance Parker
committed
Fix a data race with _swiftEmptyArrayStorage's count
1 parent d991452 commit fef19d4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,13 @@ internal struct _UnsafePartiallyInitializedContiguousArrayBuffer<Element> {
750750
_uninitializedCount: newCapacity, minimumCapacity: 0)
751751
p = newResult.firstElementAddress + result.capacity
752752
remainingCapacity = newResult.capacity - result.capacity
753-
newResult.firstElementAddress.moveInitialize(
754-
from: result.firstElementAddress, count: result.capacity)
755-
result.count = 0
753+
if !result.isEmpty {
754+
// This check prevents a data race writting to _swiftEmptyArrayStorage
755+
// Since count is always 0 there, this code does nothing anyway
756+
newResult.firstElementAddress.moveInitialize(
757+
from: result.firstElementAddress, count: result.capacity)
758+
result.count = 0
759+
}
756760
(result, newResult) = (newResult, result)
757761
}
758762
addWithExistingCapacity(element)

0 commit comments

Comments
 (0)