[PATCH 04/13] staging: wilc1000: wilc_mq_send: refactor allocation errors handling

Mike Rapoport mike.rapoport at gmail.com
Mon Sep 14 19:00:46 UTC 2015


In case the first allocation fails, we can return directly, and if the
second allocation fails we can goto to the end of the function where we
free the memory and return error.

Signed-off-by: Mike Rapoport <mike.rapoport at gmail.com>
---
 drivers/staging/wilc1000/wilc_msgqueue.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/drivers/staging/wilc1000/wilc_msgqueue.c b/drivers/staging/wilc1000/wilc_msgqueue.c
index 127b3e5..6e032b8 100644
--- a/drivers/staging/wilc1000/wilc_msgqueue.c
+++ b/drivers/staging/wilc1000/wilc_msgqueue.c
@@ -65,11 +65,13 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
-	WILC_NULLCHECK(s32RetStatus, pstrMessage);
+	if (!pstrMessage)
+		return WILC_NULL_PTR;
 	pstrMessage->u32Length = u32SendBufferSize;
 	pstrMessage->pstrNext = NULL;
 	pstrMessage->pvBuffer = kmalloc(u32SendBufferSize, GFP_ATOMIC);
-	WILC_NULLCHECK(s32RetStatus, pstrMessage->pvBuffer);
+	if (!pstrMessage->pvBuffer)
+		goto err_alloc_buffer;
 	memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
 
 	/* add it to the message queue */
@@ -90,16 +92,13 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 
 	up(&pHandle->hSem);
 
-	WILC_CATCH(s32RetStatus)
-	{
-		/* error occured, free any allocations */
-		if (pstrMessage) {
-			kfree(pstrMessage->pvBuffer);
-			kfree(pstrMessage);
-		}
-	}
-
 	return s32RetStatus;
+
+err_alloc_buffer:
+	/* error occurred, free any allocations */
+	kfree(pstrMessage);
+
+	return WILC_NULL_PTR;
 }
 
 /*!
-- 
2.1.0



More information about the devel mailing list