qemu-devel
[Top][All Lists]
Advanced

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

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


From: Peter Maydell
Subject: Re: [PATCH] hw/timer/nrf51_timer: prevent integer overflow
Date: Tue, 3 Dec 2024 16:46:29 +0000

On Tue, 3 Dec 2024 at 16:25, Anastasia Belova <abelova@astralinux.ru> wrote:
>
> 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]);

Can you explain when adding the cast makes a difference?
Since s->counter is 32 bits and ticks is 32 bits and
the RHS of the % is a power of 2, it's not clear to
me that keeping the top 32 bits in the addition and then
discarding it after the % is any different from only
taking the bottom 32 bits of the addition.

thanks
-- PMM



reply via email to

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