Skip to content

Rust programs started with closed stdio streams may corrupt files|sockets|etc #57728

Closed
@theastrallyforged

Description

@theastrallyforged

For example, if a program is started with ./foo >&- (stdout closed, stdin and stderr open), println!()s fail silently (despite strace showing that write(2) clearly returns -1 and EBADF). If a file is opened and then a println!() is attempted, it silently writes into the file (which is probably not what was intended).

Example 1:

fn main() {
    println!("Hello, world!");
}

Invocation: target/debug/hello-world >&-
Expected result: panic, with an error message relating to being unable to write to stdout, exit code 101
Observed result: nothing (no output on stderr, exit code 0)

Example 2:

fn main() {
    let file = std::fs::File::create("foo").unwrap();
    println!("Hello, world!");
    drop(file);
}

Invocation: target/debug/hello-file >&-
Expected result: panic (as before), empty file ./foo
Observed result: no panic, ./foo contains Hello, world!

Rust version: 1.30.0
Platform: x86_64-unknown-linux-gnu

It looks like any Rust program which is started with one or more closed stdio streams, opens a file (or socket, or…), and subsequently tries to read|write said stdio stream will inadvertently read|write the file instead (messing up the seek position, not to mention causing data corruption). This seems like terribly bad behavior. ;-;

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions