Skip to content
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

fix: prevent infinite loop when event reader fails to initialize #956

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions src/cursor/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
io::{self, Error, ErrorKind, Write},
time::Duration,
time::{Duration, Instant},
};

use crate::{
Expand Down Expand Up @@ -35,8 +35,10 @@ fn read_position_raw() -> io::Result<(u16, u16)> {
stdout.write_all(b"\x1B[6n")?;
stdout.flush()?;

let poll_timeout = Duration::from_millis(2000);
let poll_start = Instant::now();
loop {
match poll_internal(Some(Duration::from_millis(2000)), &CursorPositionFilter) {
match poll_internal(Some(poll_timeout), &CursorPositionFilter) {
Ok(true) => {
if let Ok(InternalEvent::CursorPosition(x, y)) =
read_internal(&CursorPositionFilter)
Expand All @@ -50,6 +52,12 @@ fn read_position_raw() -> io::Result<(u16, u16)> {
"The cursor position could not be read within a normal duration",
));
}
Err(e) if Instant::now() - poll_start > poll_timeout => {
return Err(io::Error::new(
e.kind(),
format!("Error reading cursor position: {e:?}"),
));
}
Err(_) => {}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ fn read_supports_keyboard_enhancement_raw() -> io::Result<bool> {
filter::{KeyboardEnhancementFlagsFilter, PrimaryDeviceAttributesFilter},
poll_internal, read_internal, InternalEvent,
};
use std::io::Write;
use std::time::Duration;
use std::{io::Write, time::Instant};

// This is the recommended method for testing support for the keyboard enhancement protocol.
// We send a query for the flags supported by the terminal and then the primary device attributes
Expand All @@ -229,11 +229,10 @@ fn read_supports_keyboard_enhancement_raw() -> io::Result<bool> {
stdout.flush()?;
}

let poll_timeout = Duration::from_millis(2000);
let poll_start = Instant::now();
loop {
match poll_internal(
Some(Duration::from_millis(2000)),
&KeyboardEnhancementFlagsFilter,
) {
match poll_internal(Some(poll_timeout), &KeyboardEnhancementFlagsFilter) {
Ok(true) => {
match read_internal(&KeyboardEnhancementFlagsFilter) {
Ok(InternalEvent::KeyboardEnhancementFlags(_current_flags)) => {
Expand All @@ -250,6 +249,12 @@ fn read_supports_keyboard_enhancement_raw() -> io::Result<bool> {
"The keyboard enhancement status could not be read within a normal duration",
));
}
Err(e) if Instant::now() - poll_start > poll_timeout => {
return Err(io::Error::new(
e.kind(),
format!("Error reading keyboard enhancement status: {e:?}"),
));
}
Err(_) => {}
}
}
Expand Down
Loading