Skip to content

Commit 6459199

Browse files
committed
[Data] fix crash when creating iterator of empty DispatchData
1 parent 57c5c28 commit 6459199

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/swift/Data.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,13 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
238238
var ptr: UnsafePointer<Void>?
239239
self._count = 0
240240
self._data = CDispatch.dispatch_data_create_map(_data.__wrapped, &ptr, &self._count)
241-
self._ptr = UnsafePointer(ptr!)
241+
self._ptr = ptr.flatMap { UnsafePointer($0) }
242242
self._position = _data.startIndex
243+
244+
// The only time we expect a 'nil' pointer is when the data is empty.
245+
if self._ptr == nil {
246+
assert(self._count == self._position)
247+
}
243248
}
244249

245250
/// Advance to the next element and return it, or `nil` if no next
@@ -248,13 +253,15 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
248253
/// - Precondition: No preceding call to `self.next()` has returned `nil`.
249254
public mutating func next() -> DispatchData._Element? {
250255
if _position == _count { return nil }
256+
257+
assert(_ptr != nil)
251258
let element = _ptr[_position];
252259
_position = _position + 1
253260
return element
254261
}
255262

256263
internal let _data: dispatch_data_t
257-
internal var _ptr: UnsafePointer<UInt8>
264+
internal var _ptr: UnsafePointer<UInt8>!
258265
internal var _count: Int
259266
internal var _position: DispatchData.Index
260267
}

0 commit comments

Comments
 (0)