RTEMS | stm32h7/bspstart: avoid overflow in HAL_GetTick calculation (!1144)

Mohamed Ayman (@mohamedayman23) gitlab at rtems.org
Wed Mar 18 19:06:30 UTC 2026




Mohamed Ayman commented on a discussion on bsps/arm/stm32h7/start/bspstart.c: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/1144#note_146024

 >  /* Get number of milliseconds elapsed since startup */
 >  uint32_t HAL_GetTick(void)
 >  {
 > -  return rtems_clock_get_ticks_since_boot() *
 > -    rtems_configuration_get_milliseconds_per_tick();
 > +  return (uint32_t)(
 > +    (uint64_t) rtems_clock_get_ticks_since_boot() *
 > +    rtems_configuration_get_milliseconds_per_tick()
 > +);

If either operand is a signed type the old multiplication could invoke undefined behavior on overflow, letting the compiler misoptimize. Casting to uint64_t forces unsigned arithmetic where overflow is well defined. The final truncation back to uint32_t is intentional preserving the expected wraparound of a tick counter.

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/1144#note_146024
You're receiving this email because of your account on gitlab.rtems.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20260318/82044398/attachment.htm>


More information about the bugs mailing list