Skip to content

Commit ae71a91

Browse files
authored
Merge pull request swiftlang#111 from karwa/master
[Data] fix crash when creating iterator of empty DispatchData
2 parents b84e87e + 192ad6d commit ae71a91

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/swift/Data.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,15 @@ 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 = UnsafePointer(ptr)
242242
self._position = _data.startIndex
243+
244+
// The only time we expect a 'nil' pointer is when the data is empty.
245+
assert(self._ptr != nil || self._count == self._position)
243246
}
244247

245248
/// Advance to the next element and return it, or `nil` if no next
246249
/// element exists.
247-
///
248-
/// - Precondition: No preceding call to `self.next()` has returned `nil`.
249250
public mutating func next() -> DispatchData._Element? {
250251
if _position == _count { return nil }
251252
let element = _ptr[_position];
@@ -254,7 +255,7 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
254255
}
255256

256257
internal let _data: dispatch_data_t
257-
internal var _ptr: UnsafePointer<UInt8>
258+
internal var _ptr: UnsafePointer<UInt8>!
258259
internal var _count: Int
259260
internal var _position: DispatchData.Index
260261
}

0 commit comments

Comments
 (0)