This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 72db205050870de0139ea703b8868b94863a101a Author: ouyangxiangzhen <[email protected]> AuthorDate: Thu Apr 23 21:09:44 2026 +0800 drivers/timers: fix infinite loop in up_timer_getmask when maxticks == CLOCK_MAX When maxticks equals CLOCK_MAX (all bits set), the loop that builds the mask by (*mask << 1) | 1 never terminates because the shifted value wraps around to the same mask value, making next > maxticks always false. Replace the loop with a single flsx-based expression that computes the mask directly, which naturally covers the CLOCK_MAX case. Signed-off-by: ouyangxiangzhen <[email protected]> --- drivers/timers/arch_alarm.c | 12 ++---------- drivers/timers/arch_timer.c | 13 ++----------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index b8eb29bf8af..686930c8c91 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -30,6 +30,7 @@ #include <nuttx/arch.h> #include <nuttx/clock.h> +#include <nuttx/lib/math32.h> #include <nuttx/timers/arch_alarm.h> /**************************************************************************** @@ -288,16 +289,7 @@ void weak_function up_timer_getmask(FAR clock_t *mask) ONESHOT_TICK_MAX_DELAY(g_oneshot_lower, &maxticks); - for (; ; ) - { - clock_t next = (*mask << 1) | 1; - if (next > maxticks) - { - break; - } - - *mask = next; - } + *mask = CLOCK_MAX >> (sizeof(clock_t) * 8u - flsx(maxticks)); } } diff --git a/drivers/timers/arch_timer.c b/drivers/timers/arch_timer.c index 5e3b0f988cd..7893bc8c85b 100644 --- a/drivers/timers/arch_timer.c +++ b/drivers/timers/arch_timer.c @@ -30,6 +30,7 @@ #include <nuttx/arch.h> #include <nuttx/clock.h> +#include <nuttx/lib/math32.h> #include <nuttx/timers/arch_timer.h> /**************************************************************************** @@ -280,17 +281,7 @@ void weak_function up_timer_getmask(FAR clock_t *mask) TIMER_TICK_MAXTIMEOUT(g_timer.lower, &maxticks); - *mask = 0; - while (1) - { - clock_t next = (*mask << 1) | 1; - if (next > maxticks) - { - break; - } - - *mask = next; - } + *mask = CLOCK_MAX >> (sizeof(clock_t) * 8u - flsx(maxticks)); } int weak_function up_timer_gettick(FAR clock_t *ticks)
