From 4a023ec999ce961e7c3c986bf4c0e975fee3829f Mon Sep 17 00:00:00 2001 From: Kim Topley Date: Wed, 25 Jan 2017 14:28:28 -0800 Subject: [PATCH] Fixes inccorrect behavior of DispatchData.copyBytes() when the start index is not 0 and adds a test for that method. Please enter the commit message for your changes. Lines starting --- src/swift/Data.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/swift/Data.swift b/src/swift/Data.swift index d51205ce7..30a20b8ec 100644 --- a/src/swift/Data.swift +++ b/src/swift/Data.swift @@ -135,11 +135,16 @@ public struct DispatchData : RandomAccessCollection { private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: CountableRange) { var copiedCount = 0 + if range.isEmpty { return } + let rangeSize = range.count _ = CDispatch.dispatch_data_apply(__wrapped.__wrapped) { (data: dispatch_data_t, offset: Int, ptr: UnsafeRawPointer, size: Int) in - let limit = Swift.min((range.endIndex - range.startIndex) - copiedCount, size) - memcpy(pointer + copiedCount, ptr, limit) - copiedCount += limit - return copiedCount < (range.endIndex - range.startIndex) + if offset >= range.endIndex { return false } // This region is after endIndex + let copyOffset = range.startIndex > offset ? range.startIndex - offset : 0 // offset of first byte, in this region + if copyOffset >= size { return true } // This region is before startIndex + let count = Swift.min(rangeSize - copiedCount, size - copyOffset) + memcpy(pointer + copiedCount, ptr + copyOffset, count) + copiedCount += count + return copiedCount < rangeSize } }