Skip to main content
Associate II
July 1, 2026
Question

STM32CubeMX H7 USB CDC bug: usbd_desc.c Get_SerialNum() uninitialized variables cause HardFault on -O1/-O2

  • July 1, 2026
  • 6 replies
  • 20 views

There is a critical code-generation template bug in STM32CubeMX (reproduced in 6.16.1 with STM32CubeH7) when generating USB Device middleware configurations. In the generated usbd_desc.c file, the Get_SerialNum() function is generated with uninitialized local variables that are then used in arithmetic operations.

This results in Undefined Behavior (UB). It compiles and works under -O0 because the uninitialized stack happens to evaluate to zero, but it causes an immediate, silent HardFault/BusFault during USB enumeration on Windows when compiled with -O1 or -O2 optimizations

original generated code 

static void Get_SerialNum(void)
{
uint32_t deviceserial0; // Uninitialized
uint32_t deviceserial1; // Uninitialized
uint32_t deviceserial2; // Uninitialized

// ST's generator template completely misses the lines assigning the registers!
deviceserial0 += deviceserial2; // Undefined Behavior: addition on uninitialized variables

if (deviceserial0 != 0)
{
IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
}
}
//proposed fix or at least working code
static void Get_SerialNum(void)
{
uint32_t deviceserial0;
uint32_t deviceserial1;
uint32_t deviceserial2;

/* Explicitly read the Unique Device ID (UID) from the hardware */
deviceserial0 = HAL_GetUIDw0();
deviceserial1 = HAL_GetUIDw1();
deviceserial2 = HAL_GetUIDw2();

deviceserial0 += deviceserial2;

if (deviceserial0 != 0)
{
IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
}
}

 

6 replies

Ghofrane GSOURI
ST Technical Moderator
July 1, 2026

Hello ​@ICT_TAN 

 

Could you please retest this using the latest version of STM32CubeMX 6.18.0 and confirm whether the issue still occurs?

Download STM32CubeMX6.18.0  from  here 

THX

Ghofrane

 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
ICT_TANAuthor
Associate II
July 1, 2026

Hi ​@Ghofrane GSOURI thanks for the quick reply.
Even with 1.18.0 and STM32Cube FW_H7 V1.13.0 instead of  FW_H7 V1.12.1 the error sadly still persist leaving those variables uninitialised.
Furthermore when migrating from 1.16.1 to 1.18 i now get warned, that the peripheral base clock of 240MHz should now be below or equal to 225Mhz despite showing a tooltip of 240Hz max.

Ghofrane GSOURI
ST Technical Moderator
July 1, 2026

Hello ​@ICT_TAN 

Could you please provide your IOC.

THX

Ghofrane

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
ICT_TANAuthor
Associate II
July 1, 2026

Hi ​@Ghofrane GSOURI 
sadly i did not get clearance to share the whole file, are there certain sections of relevance/importants here like only USB_DEVICE section eg.

Greetings
Jonathan

ICT_TANAuthor
Associate II
July 1, 2026

@Ghofrane GSOURI 
Though not the original project here is a .ioc of a demo project with just fs and hs enabled.
The generated code still holds the uninitialized variables.
I dug a bit further and usbd_desc_template.c holds the “correct” assignment 

deviceserial0 = *(uint32_t *)DEVICE_ID1;
deviceserial1 = *(uint32_t *)DEVICE_ID2;
deviceserial2 = *(uint32_t *)DEVICE_ID3;

 
Maybe I configured smth wrong def not a straight forward process ^^;

 

Ghofrane GSOURI
ST Technical Moderator
July 1, 2026

Hello ​@ICT_TAN 

Your contribution is much appreciated.

Issue has been escalated to dev team under internal ticker ID #0063998

I will keep you posted with updates.

THX

Ghofrane

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.