Skip to contentSkip to conversation

drivers: staging: wilc1000: Check for errors before kfree

PATCH In review Chandra S Gorentla 2 participants 5 messages Oct 2, 2015 – Oct 3, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drivers: staging: wilc1000: Check for errors before kfree

During the clean-up of the function, it is need to check if errors occurred, not the memory pointer.

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/wilc1000/wilc_msgqueue.c +1 -1
index d5ebd6d..1b73b63 100644
@@ -105,7 +105,7 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 
 ERRORHANDLER:
 	/* error occured, free any allocations */
-	if (pstrMessage) {
+	if (result) {
 		kfree(pstrMessage->pvBuffer);
 		kfree(pstrMessage);
 	}
Signature · 1 line
2.1.4
#1 lore raw

PATCH 2/3 drivers: staging: wilc1000: Remove ineffective code

The value of 'result' is not modified from 0 after initialization. Hence no need to check it.

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +0 -5
index 1b73b63..a9c28ad 100644
@@ -145,11 +145,6 @@ int wilc_mq_recv(WILC_MsgQueueHandle *pHandle,
 	down(&pHandle->hSem);
 
 	/* other non-timeout scenarios */
-	if (result) {
-		PRINT_ER("Non-timeout\n");
-		return result;
-	}
-
 	if (pHandle->bExiting) {
 		PRINT_ER("pHandle fail\n");
 		return -EFAULT;
Signature · 1 line
2.1.4
#2 lore raw

PATCH 3/3 drivers: staging: wilc1000: Do not return from function with lock is on

There are a couple of return statements before unlock. Lock is moved to a location just before queue manipulation.

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +2 -2
index a9c28ad..2fa3792 100644
@@ -72,8 +72,6 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 		goto ERRORHANDLER;
 	}
 
-	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
-
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
 	if (!pstrMessage)
@@ -88,6 +86,8 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 	memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
 
 	/* add it to the message queue */
+	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
+
 	if (!pHandle->pstrMessageList) {
 		pHandle->pstrMessageList  = pstrMessage;
 	} else {
Signature · 1 line
2.1.4
#3 lore raw
In reply to Chandra S Gorentla
Quoted 3 lines · Chandra S Gorentla

During the clean-up of the function, it is need to check if errors occurred, not the memory pointer.

The bug here is that we have a use after free on the success path. It should have been mentioned in the changelog.

Anyway, this patch is buggy. If result == -EFAULT then it will crash. Also this patch is really ugly. There is someone who is going to send a correct fix (just add a return 0).

This driver usese "do everything" style error handling. It is a bug prone anti-pattern because doing everything is more complicated than doing one thing. You can easily see it is bug prone, because it made you introduce a bug, right?

Instead the error handling should look like this:

	return 0;

err_free_msg:

	kfree(pstrMessage);

	return ret;

There are no error paths where we need to free "pstrMessage->pvBuffer" but if we were to add one it would look like this:

	return 0;

err_pvbuffer:

	kfree(pstrMessage->pvBuffer);
err_msg:
	kfree(pstrMessage);

	return ret;

This is a minimal, uncomplicated, no indenting, no if statement way of unwinding.

regards,
dan carpenter

#4 lore raw
Quoted 38 lines · Dan Carpenter
Quoted 3 lines · Chandra S Gorentla

During the clean-up of the function, it is need to check if errors occurred, not the memory pointer.

The bug here is that we have a use after free on the success path. It should have been mentioned in the changelog.

Anyway, this patch is buggy. If result == -EFAULT then it will crash. Also this patch is really ugly. There is someone who is going to send a correct fix (just add a return 0).

This driver usese "do everything" style error handling. It is a bug prone anti-pattern because doing everything is more complicated than doing one thing. You can easily see it is bug prone, because it made you introduce a bug, right?

Instead the error handling should look like this:

	return 0;

err_free_msg:

	kfree(pstrMessage);

	return ret;

There are no error paths where we need to free "pstrMessage->pvBuffer" but if we were to add one it would look like this:

	return 0;

err_pvbuffer:

	kfree(pstrMessage->pvBuffer);
err_msg:
	kfree(pstrMessage);

	return ret;
Quoted 6 lines

This is a minimal, uncomplicated, no indenting, no if statement way of unwinding.

regards,
dan carpenter

OK. There is a problem in this patch. I will correct it, reorganize the patch series.

Thank you,
chandra