qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] hw/timer/nrf51_timer: prevent integer overflow


From: Anastasia Belova
Subject: [PATCH] hw/timer/nrf51_timer: prevent integer overflow
Date: Tue, 3 Dec 2024 19:25:23 +0300

Both counter and tick are uint32_t and the result
of their addition may not fit this type. Add
explicit casting to uint64_t.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: c5a4829c08 ("hw/timer/nrf51_timer: Add nRF51 Timer peripheral")
Signed-off-by: Anastasia Belova <abelova@astralinux.ru>
---
 hw/timer/nrf51_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/timer/nrf51_timer.c b/hw/timer/nrf51_timer.c
index 35b0e62d5b..b5ff235eb8 100644
--- a/hw/timer/nrf51_timer.c
+++ b/hw/timer/nrf51_timer.c
@@ -44,7 +44,7 @@ static uint32_t update_counter(NRF51TimerState *s, int64_t 
now)
 {
     uint32_t ticks = ns_to_ticks(s, now - s->update_counter_ns);
 
-    s->counter = (s->counter + ticks) % BIT(bitwidths[s->bitmode]);
+    s->counter = ((uint64_t)s->counter + ticks) % BIT(bitwidths[s->bitmode]);
     /*
      * Only advance the sync time to the timestamp of the last tick,
      * not all the way to 'now', so we don't lose time if we do
-- 
2.47.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]