x-cube-azrtos-f4 icon indicating copy to clipboard operation
x-cube-azrtos-f4 copied to clipboard

Div by Zero Bug possible in HAL_RCC_GetSysClockFreq

Open PwnVerse opened this issue 9 months ago • 0 comments

There seems to be a bug a division by zero possible given RCC->PLLCFGR is 0. This is possible if there is a hardware fault which sets PLL Configuration register to 0.

__weak uint32_t HAL_RCC_GetSysClockFreq(void)
{
  uint32_t pllm = 0U, pllvco = 0U, pllp = 0U;
  uint32_t sysclockfreq = 0U;

  /* Get SYSCLK source -------------------------------------------------------*/
  switch (RCC->CFGR & RCC_CFGR_SWS)
  {
    case RCC_CFGR_SWS_PLL:  /* PLL used as system clock  source */
    {
      /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
      SYSCLK = PLL_VCO / PLLP */
      pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
      if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
      {
        /* HSE used as PLL clock source */
        pllvco = (uint32_t) ((((uint64_t) HSE_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
      }
  return sysclockfreq;
}

Possible Fixes

Handling the value of pllm by checking it against 0 can be one of the possible fixes.

PwnVerse avatar Jan 08 '25 23:01 PwnVerse