aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2021-10-20 09:05:44 +0200
committerUlf Lilleengen <[email protected]>2021-10-20 14:36:16 +0200
commit6c9420978b4e7a0b83d124b0e54255df4a64a9f3 (patch)
treee5d6a91b56105a7ffae6d32a58c05157c99b765b
parentacce0f1d2589033ca3b2a712e6641d62ccfa1741 (diff)
Prevent overflow in std timer driver
This prevents the std time driver from overflowing when setting the next wakeup time. If an overflow occurs, default to sleeping up to 1 second. Fixes #438
-rw-r--r--embassy/src/time/driver_std.rs8
1 files changed, 6 insertions, 2 deletions
diff --git a/embassy/src/time/driver_std.rs b/embassy/src/time/driver_std.rs
index 29911c4d2..c67884465 100644
--- a/embassy/src/time/driver_std.rs
+++ b/embassy/src/time/driver_std.rs
@@ -5,6 +5,7 @@ use std::mem::MaybeUninit;
5use std::sync::{Condvar, Mutex, Once}; 5use std::sync::{Condvar, Mutex, Once};
6use std::time::Duration as StdDuration; 6use std::time::Duration as StdDuration;
7use std::time::Instant as StdInstant; 7use std::time::Instant as StdInstant;
8use std::time::SystemTime;
8use std::{ptr, thread}; 9use std::{ptr, thread};
9 10
10use crate::time::driver::{AlarmHandle, Driver}; 11use crate::time::driver::{AlarmHandle, Driver};
@@ -63,6 +64,7 @@ impl TimeDriver {
63 } 64 }
64 65
65 fn alarm_thread() { 66 fn alarm_thread() {
67 let zero = unsafe { DRIVER.zero_instant.read() };
66 loop { 68 loop {
67 let now = DRIVER.now(); 69 let now = DRIVER.now();
68 70
@@ -86,8 +88,10 @@ impl TimeDriver {
86 } 88 }
87 } 89 }
88 90
89 let until = 91 // Ensure we don't overflow
90 unsafe { DRIVER.zero_instant.read() } + StdDuration::from_micros(next_alarm); 92 let until = zero
93 .checked_add(StdDuration::from_micros(next_alarm))
94 .unwrap_or(zero + StdDuration::from_secs(1));
91 95
92 unsafe { DRIVER.signaler.as_ref() }.wait_until(until); 96 unsafe { DRIVER.signaler.as_ref() }.wait_until(until);
93 } 97 }