Skip to content

On Unix, read_secure_line doesn't work if stdin isn't a TTY #221

@Noratrieb

Description

@Noratrieb

On Unix, read_secure_line first checks whether stdin is a TTY, and if not falls back to /dev/tty.

} else {

But when I tried

use std::error::Error;
use console::Term;

fn main() -> Result<(), Box<dyn Error>> {
    let term = Term::stdout();

    let x = term.read_secure_line()?;
    dbg!(x);
    Ok(())
}

with echo hello-world | target/debug/mytest
it hangs until I press return, after which it prints

Error: Os { code: 9, kind: Uncategorized, message: "Bad file descriptor" }

I don't think this should happen?

A bit of printf debugging revealed this line to be the source of the error

c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSAFLUSH, &original) })?;

procfs reveals that the FD has been closed at that point.

and further debugging reveals that, indeed, someone is doing a little bit of FD closing.

let read_rv = if let Some(mut f) = f_tty {

this line moves out of f_tty, which closes the File.

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