Skip to content

Fixes crash when DispatchData is created from an UnsafeBufferPointer<UInt8> with a nil address. Radar 29337927 #7194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions stdlib/public/SDK/Dispatch/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
/// - parameter bytes: A pointer to the memory. It will be copied.
/// - parameter count: The number of bytes to copy.
public init(bytes buffer: UnsafeBufferPointer<UInt8>) {
__wrapped = _swift_dispatch_data_create(
buffer.baseAddress!, buffer.count, nil, _swift_dispatch_data_destructor_default()) as! __DispatchData
__wrapped = buffer.baseAddress == nil ? _swift_dispatch_data_empty()
: _swift_dispatch_data_create(buffer.baseAddress!, buffer.count, nil,
_swift_dispatch_data_destructor_default()) as! __DispatchData
}

/// Initialize a `Data` without copying the bytes.
Expand All @@ -56,9 +57,8 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer.
public init(bytesNoCopy bytes: UnsafeBufferPointer<UInt8>, deallocator: Deallocator = .free) {
let (q, b) = deallocator._deallocator

__wrapped = _swift_dispatch_data_create(
bytes.baseAddress!, bytes.count, q, b) as! __DispatchData
__wrapped = bytes.baseAddress == nil ? _swift_dispatch_data_empty()
: _swift_dispatch_data_create(bytes.baseAddress!, bytes.count, q, b) as! __DispatchData
}

internal init(data: __DispatchData) {
Expand Down
22 changes: 22 additions & 0 deletions test/stdlib/Dispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,25 @@ DispatchAPI.test("DispatchData.copyBytes") {
expectEqual(destPtr[5], 0xFF)
}

DispatchAPI.test("DispatchData.buffers") {
let bytes = [UInt8(0), UInt8(1), UInt8(2), UInt8(2)]
var ptr = UnsafeBufferPointer<UInt8>(start: bytes, count: bytes.count)
var data = DispatchData(bytes: ptr)
expectEqual(bytes.count, data.count)
for i in 0..<data.count {
expectEqual(data[i], bytes[i])
}

data = DispatchData(bytesNoCopy: ptr, deallocator: .custom(nil, {}))
expectEqual(bytes.count, data.count)
for i in 0..<data.count {
expectEqual(data[i], bytes[i])
}

ptr = UnsafeBufferPointer<UInt8>(start: nil, count: 0)
data = DispatchData(bytes: ptr)
expectEqual(data.count, 0)

data = DispatchData(bytesNoCopy: ptr, deallocator: .custom(nil, {}))
expectEqual(data.count, 0)
}