cancel
Showing results for 
Search instead for 
Did you mean: 

Potential overflow in HAL_Delay

abellia
Visitor

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 2
MM..1
Chief III

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

 

HAL_MAX_DELAY
HAL_MIN_DELAY

 

TDK
Guru

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".