Skip to content

Commit 246bc7a

Browse files
committed
[stdlib] Fix a data race in Array initialization code
It is safe to only reset the counter when the count is non-zero. We always initialize the count to zero. This race has been reported by TSan.
1 parent d81c6ba commit 246bc7a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,10 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
539539
// unnecessary uniqueness check. We disable inlining here to curb code
540540
// growth.
541541
_buffer = ${Self}._allocateBufferUninitialized(minimumCapacity: count)
542+
_buffer.count = count
542543
}
543-
_buffer.count = count
544+
// Can’t store count here because the buffer might be pointing to the
545+
// shared empty array.
544546
}
545547

546548
/// Entry point for `Array` literal construction; builds and returns

0 commit comments

Comments
 (0)