mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drivers/usb/class/usblp.c: adjust error handling code
@ 2008-07-16 15:17 Julia Lawall
  2008-07-16 15:44 ` Oliver Neukum
  2008-07-16 16:44 ` [PATCH] " Pete Zaitcev
  0 siblings, 2 replies; 4+ messages in thread
From: Julia Lawall @ 2008-07-16 15:17 UTC (permalink / raw)
  To: zaitcev, linux-usb, linux-kernel, kernel-janitors

From: Julia Lawall <julia@diku.dk>

In this code, it is possible to tell statically whether usblp will be NULL
in the error handling code.

Another option would have been to put a label directly on the return.

The semantic match that finds this problem is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@@
identifier f,err,l,l1;
type T;
expression x,E;
statement S;
@@

x = NULL
... when != goto l1;
* x = f(...)
... when != x
err = E;
goto l;
...
* if (x != NULL)
  S
return err;
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
diff -u -p a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -1076,16 +1076,15 @@ static int usblp_probe(struct usb_interf
 		       const struct usb_device_id *id)
 {
 	struct usb_device *dev = interface_to_usbdev (intf);
-	struct usblp *usblp = NULL;
+	struct usblp *usblp;
 	int protocol;
 	int retval;
 
 	/* Malloc and start initializing usblp structure so we can use it
 	 * directly. */
-	if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
-		retval = -ENOMEM;
-		goto abort;
-	}
+	usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
+	if (!usblp)
+		return -ENOMEM;
 	usblp->dev = dev;
 	mutex_init(&usblp->wmut);
 	mutex_init (&usblp->mut);
@@ -1179,12 +1178,10 @@ abort_intfdata:
 	usb_set_intfdata (intf, NULL);
 	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
 abort:
-	if (usblp) {
-		kfree(usblp->readbuf);
-		kfree(usblp->statusbuf);
-		kfree(usblp->device_id_string);
-		kfree(usblp);
-	}
+	kfree(usblp->readbuf);
+	kfree(usblp->statusbuf);
+	kfree(usblp->device_id_string);
+	kfree(usblp);
 	return retval;
 }
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drivers/usb/class/usblp.c: adjust error handling code
  2008-07-16 15:17 [PATCH] drivers/usb/class/usblp.c: adjust error handling code Julia Lawall
@ 2008-07-16 15:44 ` Oliver Neukum
  2008-07-16 16:00   ` [PATCH retry] " Julia Lawall
  2008-07-16 16:44 ` [PATCH] " Pete Zaitcev
  1 sibling, 1 reply; 4+ messages in thread
From: Oliver Neukum @ 2008-07-16 15:44 UTC (permalink / raw)
  To: Julia Lawall; +Cc: zaitcev, linux-usb, linux-kernel, kernel-janitors

Am Mittwoch 16 Juli 2008 17:17:42 schrieb Julia Lawall:
> From: Julia Lawall <julia@diku.dk>
> 
> In this code, it is possible to tell statically whether usblp will be NULL
> in the error handling code.
> 
> Another option would have been to put a label directly on the return.

That would be better. It means less chance to overlook an error case
if locking needs to be added in the future.

	Regards
		Oliver


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH retry] drivers/usb/class/usblp.c: adjust error handling code
  2008-07-16 15:44 ` Oliver Neukum
@ 2008-07-16 16:00   ` Julia Lawall
  0 siblings, 0 replies; 4+ messages in thread
From: Julia Lawall @ 2008-07-16 16:00 UTC (permalink / raw)
  To: Oliver Neukum; +Cc: zaitcev, linux-usb, linux-kernel, kernel-janitors

From: Julia Lawall <julia@diku.dk>

In this code, it is possible to tell statically whether usblp will be NULL
in the error handling code.

Oliver Neukum suggested to make a goto to the final return rather than 
return directly.

The semantic match that finds this problem is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@@
identifier f,err,l,l1;
type T;
expression x,E;
statement S;
@@

x = NULL
... when != goto l1;
* x = f(...)
... when != x
err = E;
goto l;
...
* if (x != NULL)
  S
return err;
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
diff -u -p a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -1076,15 +1076,16 @@ static int usblp_probe(struct usb_interf
 		       const struct usb_device_id *id)
 {
 	struct usb_device *dev = interface_to_usbdev (intf);
-	struct usblp *usblp = NULL;
+	struct usblp *usblp;
 	int protocol;
 	int retval;
 
 	/* Malloc and start initializing usblp structure so we can use it
 	 * directly. */
-	if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
+	usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
+	if (!usblp) {
 		retval = -ENOMEM;
-		goto abort;
+		goto abort_ret;
 	}
 	usblp->dev = dev;
 	mutex_init(&usblp->wmut);
@@ -1179,12 +1180,11 @@ abort_intfdata:
 	usb_set_intfdata (intf, NULL);
 	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
 abort:
-	if (usblp) {
-		kfree(usblp->readbuf);
-		kfree(usblp->statusbuf);
-		kfree(usblp->device_id_string);
-		kfree(usblp);
-	}
+	kfree(usblp->readbuf);
+	kfree(usblp->statusbuf);
+	kfree(usblp->device_id_string);
+	kfree(usblp);
+abort_ret:
 	return retval;
 }
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drivers/usb/class/usblp.c: adjust error handling code
  2008-07-16 15:17 [PATCH] drivers/usb/class/usblp.c: adjust error handling code Julia Lawall
  2008-07-16 15:44 ` Oliver Neukum
@ 2008-07-16 16:44 ` Pete Zaitcev
  1 sibling, 0 replies; 4+ messages in thread
From: Pete Zaitcev @ 2008-07-16 16:44 UTC (permalink / raw)
  To: Julia Lawall; +Cc: linux-usb, linux-kernel, kernel-janitors

On Wed, 16 Jul 2008 17:17:42 +0200 (CEST), Julia Lawall <julia@diku.dk> wrote:

> In this code, it is possible to tell statically whether usblp will
> be NULL in the error handling code.

I think it's fine. Not much benefit IMHO.

-- Pete

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-07-16 16:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-16 15:17 [PATCH] drivers/usb/class/usblp.c: adjust error handling code Julia Lawall
2008-07-16 15:44 ` Oliver Neukum
2008-07-16 16:00   ` [PATCH retry] " Julia Lawall
2008-07-16 16:44 ` [PATCH] " Pete Zaitcev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome