Skip to content

Commit 86d5ba2

Browse files
author
Lance Parker
authored
Merge pull request #14778 from lancep/fighting_over_global_array_buffer_count
[stdlib] Fix a data race with _swiftEmptyArrayStorage's count
2 parents ce68190 + fef19d4 commit 86d5ba2

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)