Skip to main content
Visitor II
December 13, 2024
Question

Potential overflow in HAL_Delay

  • December 13, 2024
  • 2 replies
  • 556 views

I *think* that the check on line 7 below is to make sure that wait doesn't overflow when adding uwTickFreq. This isn't guaranteed to work unless uwTickFreq is 1.

 

 

__weak void HAL_Delay(uint32_t Delay)
{
 uint32_t tickstart = HAL_GetTick();
 uint32_t wait = Delay;

 /* Add a period to guaranty minimum wait */
 if (wait < HAL_MAX_DELAY)
 {
 wait += (uint32_t)uwTickFreq;
 }

 while ((HAL_GetTick() - tickstart) < wait)
 {
 }
}

 

 

You can do the following instead to catch the potential overflow (forgot a cast of uwTickFreq and I don't see a way to edit the code). 

 

 

__weak void HAL_Delay(uint32_t Delay)
{
 uint32_t tickstart = HAL_GetTick();
 uint32_t wait = Delay;

 /* Add a period to guaranty minimum wait */
 if (wait <= HAL_MAX_DELAY - uwTickFreq)
 {
 wait += (uint32_t)uwTickFreq;
 }

 while ((HAL_GetTick() - tickstart) < wait)
 {
 }
}

 

 

 

2 replies

MM..1
Chief III
December 13, 2024

ST here miss as is writed maximaly. Realy logic is use instead 

 

HAL_MAX_DELAY
HAL_MIN_DELAY

 

TDK
Super User
December 13, 2024

Valid bug report. Unlikely to affect anyone. Not many people are using HAL_Delay to wait 49.7 days.

"If you feel a post has answered your question, please click ""Accept as Solution""."