From b95b23ca8d45977b0bf210f46b2881bf3dfb9a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Wed, 18 Oct 2023 10:50:26 +0200 Subject: [PATCH] Fix overflow in timestamp calculation (#287) --- esp-wifi/src/timer/xtensa.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esp-wifi/src/timer/xtensa.rs b/esp-wifi/src/timer/xtensa.rs index afce7ad8138..b3cbf9a5285 100644 --- a/esp-wifi/src/timer/xtensa.rs +++ b/esp-wifi/src/timer/xtensa.rs @@ -41,7 +41,9 @@ pub fn get_systimer_count() -> u64 { overflow = TIMER_OVERFLOWS.load(Ordering::Relaxed); } - (((overflow as u64) << 32) + counter_after as u64) * 40_000_000 / 240_000_000 + // We have to precompute the divider to avoid overflow when multiplying. + const DIVIDER: u64 = 240_000_000 / TICKS_PER_SECOND; + (((overflow as u64) << 32) + counter_after as u64) / DIVIDER } pub fn setup_timer(mut timer1: TimeBase) {