Skip to contentSkip to conversation

Staging: wilc1000: Remove null check before kfree

PATCH In review Shraddha Barke 3 participants 4 messages Oct 14, 2015 – Oct 17, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Staging: wilc1000: Remove null check before kfree

kfree on NULL pointer is a no-op.
The semantic patch used to find such an instance where NULL check is present before kfree-

// <smpl>
@@ expression E; @@
- if (E != NULL) { kfree(E); }
+ kfree(E);
@@ expression E; @@
- if (E != NULL) { kfree(E); E = NULL; }
+ kfree(E);
+ E = NULL;
// </smpl>smpl>

---
 drivers/staging/wilc1000/host_interface.c | 90 +++++++++++--------------------
 1 file changed, 30 insertions(+), 60 deletions(-)
drivers/staging/wilc1000/host_interface.c +30 -60
index 04f3bc2..9e7a2b6 100644
@@ -942,22 +942,15 @@ ERRORHANDLER:
 		Handle_ScanDone(hif_drv, SCAN_EVENT_ABORTED);
 	}
 
-	if (pstrHostIFscanAttr->ch_freq_list != NULL) {
-		kfree(pstrHostIFscanAttr->ch_freq_list);
-		pstrHostIFscanAttr->ch_freq_list = NULL;
-	}
+	kfree(pstrHostIFscanAttr->ch_freq_list);
+	pstrHostIFscanAttr->ch_freq_list = NULL;
 
-	if (pstrHostIFscanAttr->ies != NULL) {
-		kfree(pstrHostIFscanAttr->ies);
-		pstrHostIFscanAttr->ies = NULL;
-	}
-	if (pstrHostIFscanAttr->hidden_network.pstrHiddenNetworkInfo != NULL)	{
-		kfree(pstrHostIFscanAttr->hidden_network.pstrHiddenNetworkInfo);
-		pstrHostIFscanAttr->hidden_network.pstrHiddenNetworkInfo = NULL;
-	}
+	kfree(pstrHostIFscanAttr->ies);
+	pstrHostIFscanAttr->ies = NULL;
+	kfree(pstrHostIFscanAttr->hidden_network.pstrHiddenNetworkInfo);
+	pstrHostIFscanAttr->hidden_network.pstrHiddenNetworkInfo = NULL;
 
-	if (pu8HdnNtwrksWidVal != NULL)
-		kfree(pu8HdnNtwrksWidVal);
+	kfree(pu8HdnNtwrksWidVal);
 
 	return s32Error;
 }
@@ -1275,10 +1268,8 @@ ERRORHANDLER:
 							       NULL,
 							       pstrHostIFconnectAttr->arg);
 			hif_drv->enuHostIFstate = HOST_IF_IDLE;
-			if (strConnectInfo.pu8ReqIEs != NULL) {
-				kfree(strConnectInfo.pu8ReqIEs);
-				strConnectInfo.pu8ReqIEs = NULL;
-			}
+			kfree(strConnectInfo.pu8ReqIEs);
+			strConnectInfo.pu8ReqIEs = NULL;
 
 		} else {
 			PRINT_ER("Connect callback function pointer is NULL\n");
@@ -1286,23 +1277,16 @@ ERRORHANDLER:
 	}
 
 	PRINT_D(HOSTINF_DBG, "Deallocating connection parameters\n");
-	if (pstrHostIFconnectAttr->bssid != NULL) {
-		kfree(pstrHostIFconnectAttr->bssid);
-		pstrHostIFconnectAttr->bssid = NULL;
-	}
+	kfree(pstrHostIFconnectAttr->bssid);
+	pstrHostIFconnectAttr->bssid = NULL;
 
-	if (pstrHostIFconnectAttr->ssid != NULL) {
-		kfree(pstrHostIFconnectAttr->ssid);
-		pstrHostIFconnectAttr->ssid = NULL;
-	}
+	kfree(pstrHostIFconnectAttr->ssid);
+	pstrHostIFconnectAttr->ssid = NULL;
 
-	if (pstrHostIFconnectAttr->ies != NULL) {
-		kfree(pstrHostIFconnectAttr->ies);
-		pstrHostIFconnectAttr->ies = NULL;
-	}
+	kfree(pstrHostIFconnectAttr->ies);
+	pstrHostIFconnectAttr->ies = NULL;
 
-	if (pu8CurrByte != NULL)
-		kfree(pu8CurrByte);
+	kfree(pu8CurrByte);
 	return s32Error;
 }
 
@@ -1393,10 +1377,8 @@ static s32 Handle_ConnectTimeout(struct host_if_drv *hif_drv)
 								   NULL,
 								   hif_drv->strWILC_UsrConnReq.u32UserConnectPvoid);
 
-		if (strConnectInfo.pu8ReqIEs != NULL) {
-			kfree(strConnectInfo.pu8ReqIEs);
-			strConnectInfo.pu8ReqIEs = NULL;
-		}
+		kfree(strConnectInfo.pu8ReqIEs);
+		strConnectInfo.pu8ReqIEs = NULL;
 	} else {
 		PRINT_ER("Connect callback function pointer is NULL\n");
 	}
@@ -1509,10 +1491,8 @@ static s32 Handle_RcvdNtwrkInfo(struct host_if_drv *hif_drv,
 	}
 
 done:
-	if (pstrRcvdNetworkInfo->buffer != NULL) {
-		kfree(pstrRcvdNetworkInfo->buffer);
-		pstrRcvdNetworkInfo->buffer = NULL;
-	}
+	kfree(pstrRcvdNetworkInfo->buffer);
+	pstrRcvdNetworkInfo->buffer = NULL;
 
 	if (pstrNetworkInfo != NULL) {
 		DeallocateNetworkInfo(pstrNetworkInfo);
@@ -1672,15 +1652,11 @@ static s32 Handle_RcvdGnrlAsyncInfo(struct host_if_drv *hif_drv,
 				gbScanWhileConnected = false;
 			}
 
-			if (strConnectInfo.pu8RespIEs != NULL) {
-				kfree(strConnectInfo.pu8RespIEs);
-				strConnectInfo.pu8RespIEs = NULL;
-			}
+			kfree(strConnectInfo.pu8RespIEs);
+			strConnectInfo.pu8RespIEs = NULL;
 
-			if (strConnectInfo.pu8ReqIEs != NULL) {
-				kfree(strConnectInfo.pu8ReqIEs);
-				strConnectInfo.pu8ReqIEs = NULL;
-			}
+			kfree(strConnectInfo.pu8ReqIEs);
+			strConnectInfo.pu8ReqIEs = NULL;
 			hif_drv->strWILC_UsrConnReq.ssidLen = 0;
 			kfree(hif_drv->strWILC_UsrConnReq.pu8ssid);
 			kfree(hif_drv->strWILC_UsrConnReq.pu8bssid);
@@ -1749,10 +1725,8 @@ static s32 Handle_RcvdGnrlAsyncInfo(struct host_if_drv *hif_drv,
 
 	}
 
-	if (pstrRcvdGnrlAsyncInfo->buffer != NULL) {
-		kfree(pstrRcvdGnrlAsyncInfo->buffer);
-		pstrRcvdGnrlAsyncInfo->buffer = NULL;
-	}
+	kfree(pstrRcvdGnrlAsyncInfo->buffer);
+	pstrRcvdGnrlAsyncInfo->buffer = NULL;
 
 	return s32Error;
 }
@@ -2874,8 +2848,7 @@ static s32 Handle_AddBASession(struct host_if_drv *hif_drv,
 	s32Error = send_config_pkt(SET_CFG, &strWID, 1,
 				   get_id_from_handler(hif_drv));
 
-	if (strWID.val != NULL)
-		kfree(strWID.val);
+	kfree(strWID.val);
 
 	return s32Error;
 
@@ -2914,8 +2887,7 @@ static s32 Handle_DelAllRxBASessions(struct host_if_drv *hif_drv,
 		PRINT_D(HOSTINF_DBG, "Couldn't delete BA Session\n");
 
 
-	if (strWID.val != NULL)
-		kfree(strWID.val);
+	kfree(strWID.val);
 
 	up(&hWaitResponse);
 
@@ -4658,11 +4630,9 @@ s32 host_int_add_beacon(struct host_if_drv *hif_drv, u32 u32Interval,
 
 ERRORHANDLER:
 	if (s32Error) {
-		if (pstrSetBeaconParam->head != NULL)
-			kfree(pstrSetBeaconParam->head);
+		kfree(pstrSetBeaconParam->head);
 
-		if (pstrSetBeaconParam->tail != NULL)
-			kfree(pstrSetBeaconParam->tail);
+		kfree(pstrSetBeaconParam->tail);
 	}
 
 	return s32Error;
Signature · 1 line
2.1.4
#1 lore raw

drivers/staging/wilc1000/host_interface.c ERRORHANDLER: · line 955

Quoted 5 lines
drivers/staging/wilc1000/host_interface.c +1 -2
-	if (pu8HdnNtwrksWidVal != NULL)
-		kfree(pu8HdnNtwrksWidVal);
+	kfree(pu8HdnNtwrksWidVal);
 
 	return s32Error;

Nothing to do with your patch, but maybe you could make a very small dent in the horrible variable names used by this driver by changing s32Error to ret.

If you want to be more ambitious, you could use Coccinelle to find a pattern like, where x is an identifier:

x = f(...);
if (\(x != 0 \| x < 0\)) { ... return ...; }

and then change all occurrences of x to ret.

Even more ambitious would be to find occurrences of

T x = ...;

and then use python to detect cases where the string represnting T is a prefix of the string representing x, and then remove T from x, and un camel case x. This would require making variables in python and sending them back to Coccinelle, which is illustrated by demos/pythontococci.cocci.

The last idea however is not necessarily the best one, because it would transform s32Error into error, and ret is much more common in the rest of the kernel. Also, you don't want to be overwhelmed with results, because you would have to check and think about each renaming to see if it is the best one.

julia

#2 lore raw
Quoted 10 lines · Julia Lawall

drivers/staging/wilc1000/host_interface.c ERRORHANDLER: · line 955

Quoted 5 lines
drivers/staging/wilc1000/host_interface.c +1 -2
-	if (pu8HdnNtwrksWidVal != NULL)
-		kfree(pu8HdnNtwrksWidVal);
+	kfree(pu8HdnNtwrksWidVal);
 
 	return s32Error;

Nothing to do with your patch, but maybe you could make a very small dent in the horrible variable names used by this driver by changing s32Error to ret.

Quoted 7 lines

If you want to be more ambitious, you could use Coccinelle to find a pattern like, where x is an identifier:

x = f(...);
if (\(x != 0 \| x < 0\)) { ... return ...; }

and then change all occurrences of x to ret.

Yeah I'll do this, wilc1000 has a lot of ugly names.

Quoted 11 lines

Even more ambitious would be to find occurrences of

T x = ...;

and then use python to detect cases where the string represnting T is a prefix of the string representing x, and then remove T from x, and un camel case x. This would require making variables in python and sending them back to Coccinelle, which is illustrated by demos/pythontococci.cocci.

Quoted 5 lines

The last idea however is not necessarily the best one, because it would transform s32Error into error, and ret is much more common in the rest of the kernel. Also, you don't want to be overwhelmed with results, because you would have to check and think about each renaming to see if it is the best one.

Yes thanks a lot :)

Quoted 3 lines

julia

maintainer
#3 lore raw
Quoted 17 lines · Shraddha Barke
Quoted 13 lines · Julia Lawall
Quoted 5 lines
drivers/staging/wilc1000/host_interface.c +1 -2
-	if (pu8HdnNtwrksWidVal != NULL)
-		kfree(pu8HdnNtwrksWidVal);
+	kfree(pu8HdnNtwrksWidVal);
	return s32Error;

Nothing to do with your patch, but maybe you could make a very small dent in the horrible variable names used by this driver by changing s32Error to ret.

If you want to be more ambitious, you could use Coccinelle to find a pattern like, where x is an identifier:

Quoted 6 lines
Quoted 4 lines

x = f(...);
if (\(x != 0 \| x < 0\)) { ... return ...; }

and then change all occurrences of x to ret.

Yeah I'll do this, wilc1000 has a lot of ugly names.

Watch out, the driver author is currently doing just that right now, I keep getting 60+ patchs every week from them, so you might run into merge issues trying to do this.

thanks,

greg k-h