Skip to content

Commit bb40547

Browse files
committed
[libdispatch-data-fixes] DispatchDataIterator crashes when iterating DispatchData.empty
* DispatchDataIterator is unsafe when iterating the empty DispatchData object as it forcibly unwraps a nil pointer.
1 parent 1872ddf commit bb40547

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

stdlib/public/SDK/Dispatch/Data.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
237237
self._count = 0
238238
self._data = __dispatch_data_create_map(
239239
_data as __DispatchData, &ptr, &self._count)
240-
self._ptr = UnsafePointer(ptr!)
240+
self._ptr = UnsafePointer(ptr)
241241
self._position = _data.startIndex
242+
assert(self._ptr != nil || self._count == self._position)
242243
}
243244

244245
/// Advance to the next element and return it, or `nil` if no next
@@ -253,7 +254,7 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
253254
}
254255

255256
internal let _data: __DispatchData
256-
internal var _ptr: UnsafePointer<UInt8>
257+
internal var _ptr: UnsafePointer<UInt8>!
257258
internal var _count: Int
258259
internal var _position: DispatchData.Index
259260
}

test/1_stdlib/Dispatch.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,10 @@ if #available(OSX 10.10, iOS 8.0, *) {
6161
block.cancel()
6262
}
6363
}
64+
65+
DispatchAPI.test("dispatch_data_t enumeration") {
66+
// Ensure we can iterate the empty iterator
67+
for x in DispatchData.empty {
68+
_ = 1
69+
}
70+
}

0 commit comments

Comments
 (0)