Skip to content

Fix infinte loop when reading from Stdin #139539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions library/std/src/sys/stdio/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ impl io::Write for Stderr {
// UTF-16 character should occupy 4 bytes at most in UTF-8
pub const STDIN_BUF_SIZE: usize = 4;

pub fn is_ebadf(_err: &io::Error) -> bool {
false
pub fn is_ebadf(err: &io::Error) -> bool {
if let Some(x) = err.raw_os_error() {
r_efi::efi::Status::NOT_READY.as_usize() == x
} else {
false
}
}

pub fn panic_output() -> Option<impl io::Write> {
Expand Down Expand Up @@ -180,13 +184,17 @@ unsafe fn simple_text_output(
fn simple_text_input_read(
stdin: *mut r_efi::protocols::simple_text_input::Protocol,
) -> io::Result<u16> {
loop {
match read_key_stroke(stdin) {
Ok(x) => return Ok(x.unicode_char),
Err(e) if e == r_efi::efi::Status::NOT_READY => wait_stdin(stdin)?,
Err(e) => return Err(io::Error::from_raw_os_error(e.as_usize())),
}
// Try reading any pending keys. Else wait for a new character
match read_key_stroke(stdin) {
Ok(x) => return Ok(x.unicode_char),
Err(e) if e == r_efi::efi::Status::NOT_READY => wait_stdin(stdin)?,
Err(e) => return Err(io::Error::from_raw_os_error(e.as_usize())),
}

// Try reading a key after the wait.
read_key_stroke(stdin)
.map(|x| x.unicode_char)
.map_err(|e| io::Error::from_raw_os_error(e.as_usize()))
}

fn wait_stdin(stdin: *mut r_efi::protocols::simple_text_input::Protocol) -> io::Result<()> {
Expand Down
Loading