Skip to content

[SR-3097] Empty DispatchData returns a nil pointer in withUnsafeBytes #713

Open
@karwa

Description

@karwa
Previous ID SR-3097
Radar None
Original Reporter @karwa
Type Bug
Environment

Swift 3

Additional Detail from JIRA
Votes 0
Component/s libdispatch
Labels Bug, RunTimeCrash
Assignee mww (JIRA)
Priority Medium

md5: b81d63c73d62a0030cc60459e73a98fc

relates to:

  • SR-2976 [Swift 4] Add an UnsafeBufferPointer.empty API

Issue Description:

I have an API which accepts a Foundation Data, and I'm trying to feed it the bytes I read from DispatchIO.read (which come packaged as a DispatchData). The following code (which is the best solution I could find to bridge from DispatchData to Foundation's Data without copying) crashes in Swift 3 when the data is empty:

let dispatchData = DispatchData.empty
dispatchData.withUnsafeBytes { (ptr: UnsafePointer<Int8>) -> Void in
    // We make the pointer mutating, but the Data is a `let` so we won't mutate the bytes
    let foundationData = Data(bytesNoCopy: UnsafeMutableRawPointer(mutating: ptr), count: dispatchData.count, deallocator: .none)
    print("\(foundationData)")
}

The root of the problem seems to be that the Swift dispatch overlay is returning a null-pointer as non-optional, and it gets dereferenced somewhere.

* thread #&#8203;1: tid = 0xda386b, 0x000000010067c557 libswiftDispatch.dylib`Dispatch.DispatchData.withUnsafeBytes <A, B> (body : (Swift.UnsafePointer<B>) throws -> A) throws -> A + 215, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
  * frame #&#8203;0: 0x000000010067c557 libswiftDispatch.dylib`Dispatch.DispatchData.withUnsafeBytes <A, B> (body : (Swift.UnsafePointer<B>) throws -> A) throws -> A + 215
    frame #&#8203;1: 0x00000001006f80f2 $__lldb_expr38`main + 242 at repl.swift:42
    frame #&#8203;2: 0x0000000100001420 repl_swift`_mh_execute_header + 5152
    frame #&#8203;3: 0x00000001000014c8 repl_swift`main + 168 at main.swift:46
    frame #&#8203;4: 0x00007fffea293255 libdyld.dylib`start + 1
    frame #&#8203;5: 0x00007fffea293255 libdyld.dylib`start + 1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions