Skip to content

Commit 3055c33

Browse files
Noratriebdjc
authored andcommitted
Don't eagerly close tty fd in read_secure
The tcsetattr was failing because the fd was closed in the if statement before, where it was moved out.
1 parent ddc83cd commit 3055c33

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/unix_term.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn terminal_size(out: &Term) -> Option<(u16, u16)> {
6767
}
6868

6969
pub fn read_secure() -> io::Result<String> {
70-
let f_tty;
70+
let mut f_tty;
7171
let fd = unsafe {
7272
if libc::isatty(libc::STDIN_FILENO) == 1 {
7373
f_tty = None;
@@ -91,14 +91,17 @@ pub fn read_secure() -> io::Result<String> {
9191
c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSAFLUSH, &termios) })?;
9292
let mut rv = String::new();
9393

94-
let read_rv = if let Some(mut f) = f_tty {
94+
let read_rv = if let Some(f) = &mut f_tty {
9595
f.read_line(&mut rv)
9696
} else {
9797
io::stdin().read_line(&mut rv)
9898
};
9999

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

102+
// Ensure the fd is only closed after everything has been restored.
103+
drop(f_tty);
104+
102105
read_rv.map(|_| {
103106
let len = rv.trim_end_matches(&['\r', '\n'][..]).len();
104107
rv.truncate(len);

0 commit comments

Comments
 (0)