From a85f4c037e02df5ea6d79f0f5f8c23a9cf37796c Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Tue, 23 Jun 2020 23:45:18 -0700 Subject: [PATCH] Remove some panics in deadline checking. (#87) time_until_deadline had a time of check to time of use problem - the deadline could pass between a call to checked_duration_since and the evaluation of `deadline - now` (which panics if the result would be negative). Resolve that by flipping the order of checked_duration_since's arguments and using the result rather than ignoring it. Also there were three places that called deadline - now(), which could panic. Replace those with time_until_deadline(). --- src/stream.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/stream.rs b/src/stream.rs index f7b4088..3716bb3 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -81,12 +81,12 @@ impl Read for DeadlineStream { // then. Otherwise return a TimedOut error. fn time_until_deadline(deadline: Instant) -> IoResult { let now = Instant::now(); - match now.checked_duration_since(deadline) { - Some(_) => Err(IoError::new( + match deadline.checked_duration_since(now) { + None => Err(IoError::new( ErrorKind::TimedOut, "timed out reading response", )), - None => Ok(deadline - now), + Some(duration) => Ok(duration), } } @@ -394,7 +394,7 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result 0 { stream @@ -406,7 +406,7 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result 0 { stream @@ -523,7 +523,9 @@ fn connect_socks5( let (lock, cvar) = &*master_signal; let done = lock.lock().unwrap(); - let done_result = cvar.wait_timeout(done, deadline - Instant::now()).unwrap(); + let done_result = cvar + .wait_timeout(done, time_until_deadline(deadline)?) + .unwrap(); let done = done_result.0; if *done { rx.recv().unwrap()?