Skip to content

Soundness: clap_lex makes incorrect assumption about OsStr #5280

Closed
@Manishearth

Description

@Manishearth

clap/clap_lex/src/ext.rs

Lines 248 to 256 in 48d28aa

// SAFETY:
// - Lifetimes are the same
// - Types are compatible (`OsStr` is effectively a transparent wrapper for `[u8]`)
// - The primary contract is that the encoding for invalid surrogate code points is not
// guaranteed which isn't a problem here
//
// There is a proposal to support this natively (https://github.com/rust-lang/rust/pull/95290)
// but its in limbo
unsafe { std::mem::transmute(s) }

It's not true that OsStr is stably a transparent wrapper around [u8], this code is technically unsound. It's fine as long as the Rust stdlib avoids making this change.

Fortunately the soon-to-be-stabilized as_os_str_bytes() will fix this. Opening this issue to track it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions