Skip to content

cast_slice is not safe #974

Closed
Closed
@bluss

Description

@bluss

Hi, I'm looking at gfx-rs and thinking of using it, but I can't drop my usual habit of reviewing unsafe code.

cast_slice is not a safe abstraction, it should not be exposed as a safe function. If you make this function private, then it's only a concern for your library internals, but it's not good that it's public.

Example breakage. usize and &T (for any T) are both Copy, but of course transmuting arbitrary integer into a reference does not make a valid reference (it may be dangling, mutabily aliased, not aligned, or null for example).

Other issues: Breaks type safety. A type may be Copy but only allow construction through explicitly exposed functions, that preserve certain invariants for example.

Recommended resolution: Demote the function to private if possible. If you need "pod casting", make an unsafe trait that is implemented for explicitly Pod compatible types, like the integer types, and only allow the cast to those types.

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