AnsweredAssumed Answered

HAL bug - array overflow in HAL_RCC_GetSysClockFreq()

Question asked by richard on Jun 10, 2016
Latest reply on Jun 16, 2016 by Antoine.PM
The purpose of this post is to save other developers the time I have spent tracking this down. Bear with me on this - its messy.

Line 1108, of stm32f1xx_hal_rcc.c is

  if (pllmul == aPLLMULFactorTable[(uint32_t)(RCC_CFGR_PLLMULL6_5) >> POSITION_VAL(RCC_CFGR_PLLMULL)])

You'll need to know that

#define  RCC_CFGR_PLLMULL6_5   ((uint32_t)0x00340000)
#define  RCC_CFGR_PLLMULL      ((uint32_t)0x003C0000)


and

const uint8_t aPLLMULFactorTable[12] = {0, 0, 4, 5, 6, 7, 8, 9, 0, 0, 0, 13};

You'll find that
(RCC_CFGR_PLLMULL6_5) >> POSITION_VAL(RCC_CFGR_PLLMULL)
is a constant that evaluates to 13.

And when this is used to index aPLLMULFactorTable[12], it generates an array bounds overflow.

This would seem to be a bug in the HAL library (though please tell me if I have got this wrong).

I encountered this in trying to understand why I was occasionally getting the wrong baudrate.

Scary, eh?





Outcomes