Skip to content

Device::from_native is unsafe #159

@Wybxc

Description

@Wybxc

The signature of Device::from_native does not constrain the relationship between the lifetimes of its input parameters and the lifetime of its return value, which may result in dangling references.

For example:

let mut custom = CustomDevice(...);
let device = Device::from_native(&mut custom);
drop(custom);
// Here `device` still holds a pointer to `custom`

One solution is to add a PhantomData containing NativeDevice to the Device to inform the compiler of the implicit lifetime requirements here.

struct Device<D> {
    // ...
    phantom: std::marker::PhantomData<D>,
}

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