Skip to content

Commit 7107f02

Browse files
committed
Use BorrowedFd in HiddenInput
1 parent 295d4f7 commit 7107f02

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/pam/rpassword.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// (although much more robust than in the original code)
1515
///
1616
use std::io::{self, Error, ErrorKind, Read};
17-
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd};
17+
use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
1818
use std::time::Instant;
1919
use std::{fs, mem};
2020

@@ -25,13 +25,13 @@ use crate::system::time::Duration;
2525

2626
use super::securemem::PamBuffer;
2727

28-
struct HiddenInput {
29-
tty: OwnedFd,
28+
struct HiddenInput<'a> {
29+
tty: BorrowedFd<'a>,
3030
term_orig: termios,
3131
}
3232

33-
impl HiddenInput {
34-
fn new(tty: OwnedFd) -> io::Result<HiddenInput> {
33+
impl<'a> HiddenInput<'a> {
34+
fn new(tty: BorrowedFd<'a>) -> io::Result<Self> {
3535
// Make two copies of the terminal settings. The first one will be modified
3636
// and the second one will act as a backup for when we want to set the
3737
// terminal back to its original state.
@@ -55,7 +55,7 @@ impl HiddenInput {
5555
}
5656
}
5757

58-
impl Drop for HiddenInput {
58+
impl Drop for HiddenInput<'_> {
5959
fn drop(&mut self) {
6060
// Set the the mode back to normal
6161
// SAFETY: we are passing tcsetattr a valid file descriptor and pointer-to-struct
@@ -285,32 +285,33 @@ impl Terminal<'_> {
285285
timeout: Option<Duration>,
286286
hidden: Hidden<()>,
287287
) -> io::Result<PamBuffer> {
288-
let do_hide_input = |input: BorrowedFd| -> Result<Hidden<HiddenInput>, io::Error> {
288+
fn do_hide_input(
289+
input: BorrowedFd,
290+
hidden: Hidden<()>,
291+
) -> Result<Hidden<HiddenInput>, io::Error> {
289292
if !safe_isatty(input) {
290293
// Input is not a tty, so we can't hide feedback.
291294
return Ok(Hidden::No);
292295
}
293296
match hidden {
294297
Hidden::No => Ok(Hidden::No),
295-
Hidden::Yes(()) => Ok(Hidden::Yes(HiddenInput::new(input.try_clone_to_owned()?)?)),
296-
Hidden::WithFeedback(()) => Ok(Hidden::WithFeedback(HiddenInput::new(
297-
input.try_clone_to_owned()?,
298-
)?)),
298+
Hidden::Yes(()) => Ok(Hidden::Yes(HiddenInput::new(input)?)),
299+
Hidden::WithFeedback(()) => Ok(Hidden::WithFeedback(HiddenInput::new(input)?)),
299300
}
300-
};
301+
}
301302

302303
match self {
303304
Terminal::StdIE(stdin, stdout) => {
304305
write_unbuffered(stdout, prompt.as_bytes())?;
305306

306-
let hide_input = do_hide_input(stdin.as_fd())?;
307+
let hide_input = do_hide_input(stdin.as_fd(), hidden)?;
307308
let mut reader = TimeoutRead::new(stdin.as_fd(), timeout);
308309
read_unbuffered(&mut reader, stdout, hide_input.as_ref())
309310
}
310311
Terminal::Tty(file) => {
311312
write_unbuffered(file, prompt.as_bytes())?;
312313

313-
let hide_input = do_hide_input(file.as_fd())?;
314+
let hide_input = do_hide_input(file.as_fd(), hidden)?;
314315
let mut reader = TimeoutRead::new(file.as_fd(), timeout);
315316
read_unbuffered(&mut reader, &mut &*file, hide_input.as_ref())
316317
}

0 commit comments

Comments
 (0)