[HAL][SPI] Check data size before changing state in reception API
diff --git a/Src/stm32l4xx_hal_spi.c b/Src/stm32l4xx_hal_spi.c
index ffcd0ab..4875c0f 100644
--- a/Src/stm32l4xx_hal_spi.c
+++ b/Src/stm32l4xx_hal_spi.c
@@ -1020,6 +1020,11 @@
     return HAL_BUSY;
   }
 
+  if ((pData == NULL) || (Size == 0U))
+  {
+    return HAL_ERROR;
+  }
+
   if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
   {
     hspi->State = HAL_SPI_STATE_BUSY_RX;
@@ -1030,11 +1035,6 @@
   /* Init tickstart for timeout management*/
   tickstart = HAL_GetTick();
 
-  if ((pData == NULL) || (Size == 0U))
-  {
-    return HAL_ERROR;
-  }
-
   /* Process Locked */
   __HAL_LOCK(hspi);
 
@@ -1687,6 +1687,11 @@
     return HAL_BUSY;
   }
 
+  if ((pData == NULL) || (Size == 0U))
+  {
+    return HAL_ERROR;
+  }
+
   if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
   {
     hspi->State = HAL_SPI_STATE_BUSY_RX;
@@ -1695,11 +1700,6 @@
   }
 
 
-  if ((pData == NULL) || (Size == 0U))
-  {
-    return HAL_ERROR;
-  }
-
   /* Process Locked */
   __HAL_LOCK(hspi);
 
@@ -2022,6 +2022,11 @@
     return HAL_BUSY;
   }
 
+  if ((pData == NULL) || (Size == 0U))
+  {
+    return HAL_ERROR;
+  }
+
   if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
   {
     hspi->State = HAL_SPI_STATE_BUSY_RX;
@@ -2033,11 +2038,6 @@
     return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
   }
 
-  if ((pData == NULL) || (Size == 0U))
-  {
-    return HAL_ERROR;
-  }
-
   /* Process Locked */
   __HAL_LOCK(hspi);