AnsweredAssumed Answered

CubeMX USB CDC wrong buffer handling?

Question asked by adib on May 26, 2015
Hello, in my earlier post I asked abouot buffer handling.

I investigated any further and found one potential error and one unused buffer.

1) buffer overflow
in file usbd_cdc_if.c
#define APP_RX_DATA_SIZE  4
...
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
...
static int8_t CDC_Init_FS(void)
{
...
  USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBufferFS);
...
}
@see USBD_CDC_SetRxBuffer doesn't take any length information

in file usbd_cdc.c this buffer is used in function:

static uint8_t  USBD_CDC_Init (USBD_HandleTypeDef *pdev,
                               uint8_t cfgidx)
{
...
      USBD_LL_PrepareReceive(pdev,
                             CDC_OUT_EP,
                             hcdc->RxBuffer,
                             CDC_DATA_HS_OUT_PACKET_SIZE);
...
}

So that means for safe operation the length of UserRxBufferFS must be minimum CDC_DATA_HS_OUT_PACKET_SIZE size.

2) unused buffer
in file usbd_cdc_if.c
#define APP_TX_DATA_SIZE  4
...
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
...
static int8_t CDC_Init_FS(void)
{
...
  USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
...
}

This buffer is never used anywhere else.
The transmit code uses the buffer the user gives to the function.
The buffer UserTxBufferFS is not needed here and misleading.

Am I right?

Best regards, Adib.

Outcomes