Skip to content

Commit 0122f3b

Browse files
committed
[HAL][SPI] Fix buffer overflow in TX interrupt service routine
1 parent 9b5af21 commit 0122f3b

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

Src/stm32h7xx_hal_spi.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,13 +3760,15 @@ static void SPI_RxISR_32BIT(SPI_HandleTypeDef *hspi)
37603760
static void SPI_TxISR_8BIT(SPI_HandleTypeDef *hspi)
37613761
{
37623762
/* Transmit data in 8 Bit mode */
3763-
*(__IO uint8_t *)&hspi->Instance->TXDR = *((const uint8_t *)hspi->pTxBuffPtr);
3764-
hspi->pTxBuffPtr += sizeof(uint8_t);
3765-
hspi->TxXferCount--;
3766-
3767-
/* Disable IT if no more data excepted */
3768-
if (hspi->TxXferCount == 0UL)
3763+
if (hspi->TxXferCount != 0UL)
3764+
{
3765+
*(__IO uint8_t *)&hspi->Instance->TXDR = *((const uint8_t *)hspi->pTxBuffPtr);
3766+
hspi->pTxBuffPtr += sizeof(uint8_t);
3767+
hspi->TxXferCount--;
3768+
}
3769+
else
37693770
{
3771+
/* Disable IT if no more data expected */
37703772
#if defined(USE_SPI_RELOAD_TRANSFER)
37713773
/* Check if there is any request to reload */
37723774
if (hspi->Reload.Requested == 1UL)
@@ -3802,19 +3804,21 @@ static void SPI_TxISR_8BIT(SPI_HandleTypeDef *hspi)
38023804
static void SPI_TxISR_16BIT(SPI_HandleTypeDef *hspi)
38033805
{
38043806
/* Transmit data in 16 Bit mode */
3807+
if (hspi->TxXferCount != 0UL)
3808+
{
38053809
#if defined (__GNUC__)
3806-
__IO uint16_t *ptxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->TXDR));
3810+
__IO uint16_t *ptxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->TXDR));
38073811

3808-
*ptxdr_16bits = *((const uint16_t *)hspi->pTxBuffPtr);
3812+
*ptxdr_16bits = *((const uint16_t *)hspi->pTxBuffPtr);
38093813
#else
3810-
*((__IO uint16_t *)&hspi->Instance->TXDR) = *((const uint16_t *)hspi->pTxBuffPtr);
3814+
*((__IO uint16 _t *)&hspi->Instance->TXDR) = *((const uint16_t *)hspi->pTxBuffPtr);
38113815
#endif /* __GNUC__ */
3812-
hspi->pTxBuffPtr += sizeof(uint16_t);
3813-
hspi->TxXferCount--;
3814-
3815-
/* Disable IT if no more data excepted */
3816-
if (hspi->TxXferCount == 0UL)
3816+
hspi->pTxBuffPtr += sizeof(uint16_t);
3817+
hspi->TxXferCount--;
3818+
}
3819+
else
38173820
{
3821+
/* Disable IT if no more data expected */
38183822
#if defined(USE_SPI_RELOAD_TRANSFER)
38193823
/* Check if there is any request to reload */
38203824
if (hspi->Reload.Requested == 1UL)
@@ -3850,13 +3854,15 @@ static void SPI_TxISR_16BIT(SPI_HandleTypeDef *hspi)
38503854
static void SPI_TxISR_32BIT(SPI_HandleTypeDef *hspi)
38513855
{
38523856
/* Transmit data in 32 Bit mode */
3853-
*((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
3854-
hspi->pTxBuffPtr += sizeof(uint32_t);
3855-
hspi->TxXferCount--;
3856-
3857-
/* Disable IT if no more data excepted */
3858-
if (hspi->TxXferCount == 0UL)
3857+
if (hspi->TxXferCount != 0UL)
3858+
{
3859+
*((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
3860+
hspi->pTxBuffPtr += sizeof(uint32_t);
3861+
hspi->TxXferCount--;
3862+
}
3863+
else
38593864
{
3865+
/* Disable IT if no more data expected */
38603866
#if defined(USE_SPI_RELOAD_TRANSFER)
38613867
/* Check if there is any request to reload */
38623868
if (hspi->Reload.Requested == 1UL)

0 commit comments

Comments
 (0)