Skip to content

Commit 302d827

Browse files
committed
[Foundation] Address exclusivity violation in Data
Address an exclusivity violation in Data's Iterator.next() by changing two private 'let' stored properties to be 'var'. Making the properties 'var' changes code generation of next() so that the stored properties are read independently of the other contents of the struct. This prevents an exclusivity violation when reading '_endIdx' and '_data' while simultaneously mutating '_buffer' with the call to withUnsafeMutablePointer(). The 'let' pattern is an idiom we would eventually like to support (see SR-7396), but for now we need to remove the exclusivity violation.
1 parent bd3e488 commit 302d827

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,14 +1762,23 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
17621762
}
17631763

17641764
public struct Iterator : IteratorProtocol {
1765-
private let _data: Data
1765+
// Both _data and _endIdx should be 'let' rather than 'var'.
1766+
// They are 'var' so that the stored properties can be read
1767+
// independently of the other contents of the struct. This prevents
1768+
// an exclusivity violation when reading '_endIdx' and '_data'
1769+
// while simultaneously mutating '_buffer' with the call to
1770+
// withUnsafeMutablePointer(). Once we support accessing struct
1771+
// let properties independently we should make these variables
1772+
// 'let' again.
1773+
1774+
private var _data: Data
17661775
private var _buffer: (
17671776
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
17681777
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
17691778
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
17701779
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
17711780
private var _idx: Data.Index
1772-
private let _endIdx: Data.Index
1781+
private var _endIdx: Data.Index
17731782

17741783
fileprivate init(_ data: Data) {
17751784
_data = data

0 commit comments

Comments
 (0)