STM USB HAL library HardFault bug
I decided to use the HAL USB library to handle communication with a PC (CDC class implementation) in the F303 processor. I ran into a strange issue: after a PC reboot device was freezing every time.
A quick look into debugger revealed that
HardFault exception is raised. After stack trace investigation it turned out that the scenario of the fail was pretty simple.
- After close command in OS, USB detaches all devices.
- Interrupt from USB bus is fired and
USBD_CDC_DeInitcallback is executed.
- Library is closing bus and freeing all resources.
Program fails at freeing memory (
line 585, usbd_cdc.c):
When you trace this function it leads to simple
free() instruction, nothing fancy. But when you look up a little bit you will find a function which should allocate memory in a way that could be released through
... but it is not. It just returns a pointer to a static variable, so there is nothing to be
free() at the end!
Quick look at free function reference:
Deallocate memory block A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated, making it available again for further allocations. If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.
Just comment out
This bug is present in file version presented below.