From 86860b50fc8467cd8028f3581e3a55512bfed74e Mon Sep 17 00:00:00 2001 From: Kim Topley Date: Wed, 1 Feb 2017 16:31:15 -0800 Subject: [PATCH 1/3] Fixes crash when DispatchData is created from an UnsafeBufferPointer with a nil address. --- src/swift/Data.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/swift/Data.swift b/src/swift/Data.swift index d51205ce7..e0647e743 100644 --- a/src/swift/Data.swift +++ b/src/swift/Data.swift @@ -50,7 +50,9 @@ public struct DispatchData : RandomAccessCollection { /// /// - parameter bytes: A pointer to the memory. It will be copied. public init(bytes buffer: UnsafeBufferPointer) { - 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 @@ public struct DispatchData : RandomAccessCollection { /// - parameter deallocator: Specifies the mechanism to free the indicated buffer. public init(bytesNoCopy bytes: UnsafeBufferPointer, 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) } From ca29f4073aabe03418aa91e49adb34bad669f0e9 Mon Sep 17 00:00:00 2001 From: Kim Topley Date: Wed, 1 Feb 2017 16:37:54 -0800 Subject: [PATCH 2/3] Revert "Fixes crash when DispatchData is created from an UnsafeBufferPointer with a nil address." This reverts commit 86860b50fc8467cd8028f3581e3a55512bfed74e (fix should have been made in a branch) --- src/swift/Data.swift | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/swift/Data.swift b/src/swift/Data.swift index e0647e743..d51205ce7 100644 --- a/src/swift/Data.swift +++ b/src/swift/Data.swift @@ -50,9 +50,7 @@ public struct DispatchData : RandomAccessCollection { /// /// - parameter bytes: A pointer to the memory. It will be copied. public init(bytes buffer: UnsafeBufferPointer) { - let d = buffer.baseAddress == nil ? _swift_dispatch_data_empty() - : dispatch_data_create(buffer.baseAddress!, buffer.count, nil, - _dispatch_data_destructor_default()) + let d = dispatch_data_create(buffer.baseAddress!, buffer.count, nil, _dispatch_data_destructor_default()) self.init(data: d) } @@ -62,8 +60,7 @@ public struct DispatchData : RandomAccessCollection { /// - parameter deallocator: Specifies the mechanism to free the indicated buffer. public init(bytesNoCopy bytes: UnsafeBufferPointer, deallocator: Deallocator = .free) { let (q, b) = deallocator._deallocator - let d = bytes.baseAddress == nil ? _swift_dispatch_data_empty() - : dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b) + let d = dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b) self.init(data: d) } From fe3d6775643ce3088ca41396f4e73113c87011ec Mon Sep 17 00:00:00 2001 From: Kim Topley Date: Wed, 1 Feb 2017 16:39:51 -0800 Subject: [PATCH 3/3] Fixes crash when DispatchData is created from an UnsafeBufferPointer with a nil address. Radar 29337927 --- src/swift/Data.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/swift/Data.swift b/src/swift/Data.swift index d51205ce7..e0647e743 100644 --- a/src/swift/Data.swift +++ b/src/swift/Data.swift @@ -50,7 +50,9 @@ public struct DispatchData : RandomAccessCollection { /// /// - parameter bytes: A pointer to the memory. It will be copied. public init(bytes buffer: UnsafeBufferPointer) { - 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 @@ public struct DispatchData : RandomAccessCollection { /// - parameter deallocator: Specifies the mechanism to free the indicated buffer. public init(bytesNoCopy bytes: UnsafeBufferPointer, 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) }