Skip to content

Some code is unsafe but not marked as unsafe #33

@Woyten

Description

@Woyten

This example segfaults:

let output_port = {
    let context = PortMidi::new().unwrap();
    let device = context.device(device_number).unwrap();
    context.output_port(device, 1024).unwrap()
};

output_port.write_message(message); // SEGFAULT

The problem is that output_port outlives context. To be more precise, context calls Pm_Terminate in its destructor rendering output_port invalid. But, unfortunately, outport_port is still accessible after the scope braces. The problem can be solved by adding an artificial lifetime to the DeviceInfo struct, s.t. Rust knows that output_port depends on a DeviceInfo instance which depends on context. Also, DeviceInfo::new should be unsafe and PortMidi::device should validate the device number.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions