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
[SR-4028] enumerateBytes gives 0 length with swift 3.1 #3886
Comments
cc @phausler |
If I'm reading the code correctly, that parameter isn't supposed to be the length; it's the offset. The fact that it used to provide the length (and still does for certain Data instances) is a bug. |
@belkadan The documentation also doesn't really help much. What is the purpose of the index? Should it be used to offset the UnsafeBufferPointer.baseAddress? Thanks |
@phausler In the current code it is either hard coded to 0 or is range.length. public func enumerateBytes(_ block: (_ buffer: UnsafeBufferPointer<UInt8>, _ byteIndex: Data.Index, _ stop: inout Bool) -> Void) {
var stop: Bool = false
switch _backing {
case .swift:
block(UnsafeBufferPointer<UInt8>(start: _bytes?.assumingMemoryBound(to: UInt8.self), count: _length), 0, &stop)
case .immutable:
block(UnsafeBufferPointer<UInt8>(start: _bytes?.assumingMemoryBound(to: UInt8.self), count: _length), 0, &stop)
case .mutable:
block(UnsafeBufferPointer<UInt8>(start: _bytes?.assumingMemoryBound(to: UInt8.self), count: _length), 0, &stop)
case .customReference(let d):
d.enumerateBytes { (ptr, range, stop) in
var stopv = false
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: range.length)
block(UnsafeBufferPointer(start: bytePtr, count: range.length), range.length, &stopv)
if stopv {
stop.pointee = true
}
}
case .customMutableReference(let d):
d.enumerateBytes { (ptr, range, stop) in
var stopv = false
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: range.length)
block(UnsafeBufferPointer(start: bytePtr, count: range.length), range.length, &stopv)
if stopv {
stop.pointee = true
}
}
}
} |
the byteIndex should be the start of the region. So this is a typo that custom references ought to be the range.location not the range.length for the byteIndex parameter. Thankfully these are relatively rare in the SDK. In the interim the byte index can be calculated by summing the counts of the data while iterating (it so happens that regions are ordered) |
@nevil is there an associated radar with this? |
@phausler Thanks for the response! Should I create one? |
Confirmed fixed in swift-4.0-DEVELOPMENT-SNAPSHOT-2017-05-11 |
Additional Detail from JIRA
md5: 682108fbbdcf9923fa882f574139be25
Issue Description:
Problem:
enumerateBytes receives length value 0 when compiled with swift 3.1 from Xcode 8.3 beta 3.
With swift 3.0.1 from Xcode 8.2.1 the length is the correct length of the data buffer.
See the code at the end of the report.
Not expected behavior:
Command line tools set to "Xcode 8.3 (8W132p)" in Xcode
Expected behavior:
Command line tools set to "Xcode 8.2.1 (8C1002)" in Xcode
Test code, bug.swift
The text was updated successfully, but these errors were encountered: