HAL_Delay function corner case not covered
Hi there,
this is the default implementation of HAL_Delay function for stm32h7xx_hal.c (and I assume many other hals too)
__weak void HAL_Delay(uint32_t Delay)
{
uint32_t tickstart = HAL_GetTick();
uint32_t wait = Delay;
/* Add a freq to guarantee minimum wait */
if (wait < HAL_MAX_DELAY)
{
wait += (uint32_t)(uwTickFreq);
}
while ((HAL_GetTick() - tickstart) < wait)
{
}
}The issue here is that it does not cover the overflow corner case.
Let's say that Delay =100 and tickstart = HAL_GetTick() happens to be 0xFFFFFFFE.
This will cause an infinite loop.
If the tick gets incremented by 1ms this overflow happens at leats once every 50 hours.
Where could I add suggestion for code changes? A simple check as this could solve the issue.
__weak void HAL_Delay(uint32_t Delay)
{
uint32_t tickstart = HAL_GetTick();
uint32_t wait = Delay;
/* Add a freq to guarantee minimum wait */
if (wait < HAL_MAX_DELAY)
{
wait += (uint32_t)(uwTickFreq);
}
// Overflow case
if (tickstart < (tickstart + wait))
{
while(HAL_GetTick() >= tickstart)
{
}
wait = 0xFFFFFFFF - tickstart;
}
while ((HAL_GetTick() - tickstart) < wait)
{
}
}Thanks
