aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-10-20 13:16:25 +0000
committerGitHub <[email protected]>2021-10-20 13:16:25 +0000
commita895b6351f68c1af104d2d65e6df121b4a100808 (patch)
treed83ff5e3e5ce176df1e81d5549ce7bd8c4a22a4b
parentacce0f1d2589033ca3b2a712e6641d62ccfa1741 (diff)
parent5e6ee59ecd255c1d25bb0f687240183b63c4887d (diff)
Merge #439
439: Prevent overflow in std timer driver r=lulf a=lulf 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 Co-authored-by: Ulf Lilleengen <[email protected]>
-rw-r--r--embassy/src/time/driver_std.rs7
1 files changed, 5 insertions, 2 deletions
diff --git a/embassy/src/time/driver_std.rs b/embassy/src/time/driver_std.rs
index 29911c4d2..0b5c6f85c 100644
--- a/embassy/src/time/driver_std.rs
+++ b/embassy/src/time/driver_std.rs
@@ -63,6 +63,7 @@ impl TimeDriver {
63 } 63 }
64 64
65 fn alarm_thread() { 65 fn alarm_thread() {
66 let zero = unsafe { DRIVER.zero_instant.read() };
66 loop { 67 loop {
67 let now = DRIVER.now(); 68 let now = DRIVER.now();
68 69
@@ -86,8 +87,10 @@ impl TimeDriver {
86 } 87 }
87 } 88 }
88 89
89 let until = 90 // Ensure we don't overflow
90 unsafe { DRIVER.zero_instant.read() } + StdDuration::from_micros(next_alarm); 91 let until = zero
92 .checked_add(StdDuration::from_micros(next_alarm))
93 .unwrap_or_else(|| StdInstant::now() + StdDuration::from_secs(1));
91 94
92 unsafe { DRIVER.signaler.as_ref() }.wait_until(until); 95 unsafe { DRIVER.signaler.as_ref() }.wait_until(until);
93 } 96 }