Fixes crash when DispatchData is created from an UnsafeBufferPointer<Uint8> with a nil address.
Radar 29337927
diff --git a/src/swift/Data.swift b/src/swift/Data.swift
index d51205c..e0647e7 100644
--- a/src/swift/Data.swift
+++ b/src/swift/Data.swift
@@ -50,7 +50,9 @@
///
/// - parameter bytes: A pointer to the memory. It will be copied.
public init(bytes buffer: UnsafeBufferPointer<UInt8>) {
- let d = dispatch_data_create(buffer.baseAddress!, buffer.count, nil, _dispatch_data_destructor_default())
+ let d = buffer.baseAddress == nil ? _swift_dispatch_data_empty()
+ : dispatch_data_create(buffer.baseAddress!, buffer.count, nil,
+ _dispatch_data_destructor_default())
self.init(data: d)
}
@@ -60,7 +62,8 @@
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer.
public init(bytesNoCopy bytes: UnsafeBufferPointer<UInt8>, deallocator: Deallocator = .free) {
let (q, b) = deallocator._deallocator
- let d = dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b)
+ let d = bytes.baseAddress == nil ? _swift_dispatch_data_empty()
+ : dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b)
self.init(data: d)
}