Skip to content

Fixes incorrect behavior of DispatchData.copyBytes() when the start … #204

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
Jan 30, 2017
Merged
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
13 changes: 9 additions & 4 deletions src/swift/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ public struct DispatchData : RandomAccessCollection {

private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: CountableRange<Index>) {
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
}
}

Expand Down