Skip to content

Commit 10eb0e4

Browse files
authored
Merge pull request #208 from ktopley-apple/dispatch-data-null-buffer
Fixes crash when DispatchData is created from an UnsafeBufferPointer<UInt8> with a nil address (Radar 29337927)
2 parents 3767ac7 + fe3d677 commit 10eb0e4

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/swift/Data.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public struct DispatchData : RandomAccessCollection {
5050
///
5151
/// - parameter bytes: A pointer to the memory. It will be copied.
5252
public init(bytes buffer: UnsafeBufferPointer<UInt8>) {
53-
let d = dispatch_data_create(buffer.baseAddress!, buffer.count, nil, _dispatch_data_destructor_default())
53+
let d = buffer.baseAddress == nil ? _swift_dispatch_data_empty()
54+
: dispatch_data_create(buffer.baseAddress!, buffer.count, nil,
55+
_dispatch_data_destructor_default())
5456
self.init(data: d)
5557
}
5658

@@ -60,7 +62,8 @@ public struct DispatchData : RandomAccessCollection {
6062
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer.
6163
public init(bytesNoCopy bytes: UnsafeBufferPointer<UInt8>, deallocator: Deallocator = .free) {
6264
let (q, b) = deallocator._deallocator
63-
let d = dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b)
65+
let d = bytes.baseAddress == nil ? _swift_dispatch_data_empty()
66+
: dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b)
6467
self.init(data: d)
6568
}
6669

0 commit comments

Comments
 (0)