Skip to content

Commit 375f289

Browse files
committed
stdlib: fix ARC for getting an ArraySlice of an CocoaArray with non-contiguous storage.
For this special case we copied the objects out of the cocoa array without retaining them. This lead to a double-free crash. Unfortunately I could not come up with an isolated test case. rdar://74624065
1 parent 107add7 commit 375f289

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

stdlib/public/core/CocoaArray.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ internal struct _CocoaArrayWrapper: RandomAccessCollection {
8383
let result = _ContiguousArrayBuffer<AnyObject>(
8484
_uninitializedCount: boundsCount,
8585
minimumCapacity: 0)
86-
87-
// Tell Cocoa to copy the objects into our storage
88-
core.getObjects(
89-
UnsafeMutableRawPointer(result.firstElementAddress)
90-
.assumingMemoryBound(to: AnyObject.self),
91-
range: _SwiftNSRange(location: bounds.lowerBound, length: boundsCount))
86+
87+
let base = UnsafeMutableRawPointer(result.firstElementAddress)
88+
.assumingMemoryBound(to: AnyObject.self)
89+
90+
for idx in 0..<boundsCount {
91+
(base + idx).initialize(to: core.objectAt(idx + bounds.lowerBound))
92+
}
9293

9394
return _SliceBuffer(_buffer: result, shiftedToStartIndex: bounds.lowerBound)
9495
}

0 commit comments

Comments
 (0)