Skip to content

dd: remove unsafe from stdin_as_file() by rustix::{io::stdin,stdio::stdin} #12212

@oech3

Description

@oech3

This fn is used just for avoiding overreading from stdin. But the unsafe can be logically removed by rustix's read syscall wrapper (at least on unix and wasi).
So we don't need have both of unexpected closing of fd and additional syscall overhead caused by clone.

/// Create a source from stdin using its raw file descriptor.
///
/// This returns an instance of the `Source::StdinFile` variant,
/// using the raw file descriptor of [`io::Stdin`] to create
/// the [`File`] parameter. You can use this instead of
/// `Source::Stdin` to allow reading from stdin without consuming
/// the entire contents of stdin when this process terminates.
#[cfg(unix)]
fn stdin_as_file() -> Self {
let fd = io::stdin().as_raw_fd();
let f = unsafe { File::from_raw_fd(fd) };
Self::StdinFile(f)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions