diff mbox series

Staging: irda: Do not check for NOT NULL before kfree()

Message ID 1513624290-2965-1-git-send-email-shreeya.patel23498@gmail.com
State Not Applicable, archived
Delegated to: David Miller
Headers show
Series Staging: irda: Do not check for NOT NULL before kfree() | expand

Commit Message

Shreeya Patel Dec. 18, 2017, 7:11 p.m. UTC
Do not check for NOT NULL before calling kfree because if the
pointer is NULL, no action occurs.
Done using the following semantic patch by coccinelle.

@@
expression ptr;
@@

- if (ptr != NULL) {
  kfree(ptr);
  ptr = NULL;
- }

The semantic patch has the effect of adding an assignment
of ptr to NULL in the case where ptr is NULL already.

Signed-off-by: Shreeya Patel <shreeya.patel23498@gmail.com>
---
 drivers/staging/irda/net/irnet/irnet_irda.c | 28 ++++++++++------------------
 drivers/staging/irda/net/irnet/irnet_ppp.c  | 10 ++++------
 2 files changed, 14 insertions(+), 24 deletions(-)

Comments

Stephen Hemminger Dec. 18, 2017, 7:20 p.m. UTC | #1
On Tue, 19 Dec 2017 00:41:30 +0530
Shreeya Patel <shreeya.patel23498@gmail.com> wrote:

> Do not check for NOT NULL before calling kfree because if the
> pointer is NULL, no action occurs.
> Done using the following semantic patch by coccinelle.
> 
> @@
> expression ptr;
> @@
> 
> - if (ptr != NULL) {
>   kfree(ptr);
>   ptr = NULL;
> - }
> 
> The semantic patch has the effect of adding an assignment
> of ptr to NULL in the case where ptr is NULL already.
> 
> Signed-off-by: Shreeya Patel <shreeya.patel23498@gmail.com>

Please read drivers/staging/irda/TODO
Shreeya Patel Dec. 18, 2017, 7:27 p.m. UTC | #2
On Mon, 2017-12-18 at 11:20 -0800, Stephen Hemminger wrote:
> On Tue, 19 Dec 2017 00:41:30 +0530
> Shreeya Patel <shreeya.patel23498@gmail.com> wrote:
> 
> > 
> > Do not check for NOT NULL before calling kfree because if the
> > pointer is NULL, no action occurs.
> > Done using the following semantic patch by coccinelle.
> > 
> > @@
> > expression ptr;
> > @@
> > 
> > - if (ptr != NULL) {
> >   kfree(ptr);
> >   ptr = NULL;
> > - }
> > 
> > The semantic patch has the effect of adding an assignment
> > of ptr to NULL in the case where ptr is NULL already.
> > 
> > Signed-off-by: Shreeya Patel <shreeya.patel23498@gmail.com>
> Please read drivers/staging/irda/TODO

Oh, I was not knowing about it.

Thank you
>
diff mbox series

Patch

diff --git a/drivers/staging/irda/net/irnet/irnet_irda.c b/drivers/staging/irda/net/irnet/irnet_irda.c
index e390bce..9fb79fe 100644
--- a/drivers/staging/irda/net/irnet/irnet_irda.c
+++ b/drivers/staging/irda/net/irnet/irnet_irda.c
@@ -659,13 +659,9 @@  irda_irnet_destroy(irnet_socket *	self)
       self->iriap = NULL;
     }
 
-  /* Cleanup eventual discoveries from connection attempt or control channel */
-  if(self->discoveries != NULL)
-    {
-      /* Cleanup our copy of the discovery log */
-      kfree(self->discoveries);
-      self->discoveries = NULL;
-    }
+  /* Cleanup our copy of the discovery log */
+  kfree(self->discoveries);
+  self->discoveries = NULL;
 
   /* Close our IrTTP connection */
   if(self->tsap)
@@ -874,11 +870,9 @@  irnet_connect_socket(irnet_socket *	server,
       iriap_close(new->iriap);
       new->iriap = NULL;
     }
-  if(new->discoveries != NULL)
-    {
-      kfree(new->discoveries);
-      new->discoveries = NULL;
-    }
+
+  kfree(new->discoveries);
+  new->discoveries = NULL;
 
 #ifdef CONNECT_INDIC_KICK
   /* As currently we don't block packets in ppp_irnet_send() while passive,
@@ -1605,12 +1599,10 @@  irnet_discovervalue_confirm(int		result,
   /* No more items : remove the log and signal termination */
   DEBUG(IRDA_OCB_INFO, "Cleaning up log (0x%p)\n",
 	self->discoveries);
-  if(self->discoveries != NULL)
-    {
-      /* Cleanup our copy of the discovery log */
-      kfree(self->discoveries);
-      self->discoveries = NULL;
-    }
+
+  /* Cleanup our copy of the discovery log */
+  kfree(self->discoveries);
+  self->discoveries = NULL;
   self->disco_number = -1;
 
   /* Check out what we found */
diff --git a/drivers/staging/irda/net/irnet/irnet_ppp.c b/drivers/staging/irda/net/irnet/irnet_ppp.c
index 7025dcb..855ce58 100644
--- a/drivers/staging/irda/net/irnet/irnet_ppp.c
+++ b/drivers/staging/irda/net/irnet/irnet_ppp.c
@@ -259,12 +259,10 @@  irnet_read_discovery_log(irnet_socket *ap, char *event, int buf_size)
       /* No more items : remove the log and signal termination */
       DEBUG(CTRL_INFO, "Cleaning up log (0x%p)\n",
 	    ap->discoveries);
-      if(ap->discoveries != NULL)
-	{
-	  /* Cleanup our copy of the discovery log */
-	  kfree(ap->discoveries);
-	  ap->discoveries = NULL;
-	}
+
+      /* Cleanup our copy of the discovery log */
+      kfree(ap->discoveries);
+      ap->discoveries = NULL;
       ap->disco_number = -1;
     }