Skip to contentSkip to conversation

USB hub_probe: remove ugly goto-into-compound-statement

PATCH Superseded Eugene Korenevsky 2 participants 3 messages Nov 2, 2016 – Nov 7, 2016 USB

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 USB hub_probe: remove ugly goto-into-compound-statement

Rework smelling code (goto inside compound statement). Perhaps this is legacy. Anyway such code is not appropriate for Linux kernel.

---
 drivers/usb/core/hub.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
drivers/usb/core/hub.c +11 -13
index cbb1467..4081672 100644
@@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
 
 	/* Some hubs have a subclass of 1, which AFAICT according to the */
 	/*  specs is not defined, but it works */
-	if ((desc->desc.bInterfaceSubClass != 0) &&
-	    (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+
+	/* Reject in following cases:
+	 * - Interface subclass is not 0 or 1
+	 * - Multiple endpoints
+	 * - Not an interrupt in endpoint
+	 */
+	endpoint = &desc->endpoint[0].desc;
+	if ((desc->desc.bInterfaceSubClass != 0 &&
+	     desc->desc.bInterfaceSubClass != 1) ||
+	    desc->desc.bNumEndpoints != 1 ||
+	    !usb_endpoint_is_int_in(endpoint)) {
 		dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
 		return -EIO;
 	}
 
-	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
-	if (desc->desc.bNumEndpoints != 1)
-		goto descriptor_error;
-
-	endpoint = &desc->endpoint[0].desc;
-
-	/* If it's not an interrupt in endpoint, we'd better punt! */
-	if (!usb_endpoint_is_int_in(endpoint))
-		goto descriptor_error;
-
 	/* We found a hub */
 	dev_info(&intf->dev, "USB hub found\n");
 
Signature · 1 line
2.10.2
#1 lore raw
Quoted 7 lines · Eugene Korenevsky

Rework smelling code (goto inside compound statement). Perhaps this is legacy. Anyway such code is not appropriate for Linux kernel.

---
 drivers/usb/core/hub.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

What changed from v1?

Quoted 27 lines
drivers/usb/core/hub.c +11 -3
index cbb1467..4081672 100644
@@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
 
 	/* Some hubs have a subclass of 1, which AFAICT according to the */
 	/*  specs is not defined, but it works */
-	if ((desc->desc.bInterfaceSubClass != 0) &&
-	    (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+
+	/* Reject in following cases:
+	 * - Interface subclass is not 0 or 1
+	 * - Multiple endpoints
+	 * - Not an interrupt in endpoint
+	 */
+	endpoint = &desc->endpoint[0].desc;
+	if ((desc->desc.bInterfaceSubClass != 0 &&
+	     desc->desc.bInterfaceSubClass != 1) ||
+	    desc->desc.bNumEndpoints != 1 ||
+	    !usb_endpoint_is_int_in(endpoint)) {
 		dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
 		return -EIO;
 	}
 

drivers/usb/core/hub.c static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) · line 1821

Quoted 10 lines
drivers/usb/core/hub.c +0 -10
-	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
-	if (desc->desc.bNumEndpoints != 1)
-		goto descriptor_error;
-
-	endpoint = &desc->endpoint[0].desc;
-
-	/* If it's not an interrupt in endpoint, we'd better punt! */
-	if (!usb_endpoint_is_int_in(endpoint))
-		goto descriptor_error;
-

As "horrible" as the original code might be, it's much easier to read and follow, which is the key thing here, right? What's so bad about a goto backwards?

thanks,

greg k-h

#2 lore raw
Quoted 9 lines · Greg Kroah-Hartman
Quoted 7 lines

Rework smelling code (goto inside compound statement). Perhaps this is legacy. Anyway such code is not appropriate for Linux kernel.

---
 drivers/usb/core/hub.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

What changed from v1?

Fixed faults: missed 'Signed-off-by', spaces instead of tab.

Quoted 26 lines
Quoted 26 lines
drivers/usb/core/hub.c +11 -3
index cbb1467..4081672 100644
@@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
 
 	/* Some hubs have a subclass of 1, which AFAICT according to the */
 	/*  specs is not defined, but it works */
-	if ((desc->desc.bInterfaceSubClass != 0) &&
-	    (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+
+	/* Reject in following cases:
+	 * - Interface subclass is not 0 or 1
+	 * - Multiple endpoints
+	 * - Not an interrupt in endpoint
+	 */
+	endpoint = &desc->endpoint[0].desc;
+	if ((desc->desc.bInterfaceSubClass != 0 &&
+	     desc->desc.bInterfaceSubClass != 1) ||
+	    desc->desc.bNumEndpoints != 1 ||
+	    !usb_endpoint_is_int_in(endpoint)) {
 		dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
 		return -EIO;
 	}
 
Quoted 14 lines

drivers/usb/core/hub.c static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) · line 1821

Quoted 10 lines
drivers/usb/core/hub.c +0 -10
-	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
-	if (desc->desc.bNumEndpoints != 1)
-		goto descriptor_error;
-
-	endpoint = &desc->endpoint[0].desc;
-
-	/* If it's not an interrupt in endpoint, we'd better punt! */
-	if (!usb_endpoint_is_int_in(endpoint))
-		goto descriptor_error;
-

As "horrible" as the original code might be, it's much easier to read and follow, which is the key thing here, right? What's so bad about a goto backwards?

OK, this patch is still not perfect. But jumping *back* into *compound* *statement* hurts reader's eyes. Really. Maybe it's better to extract this code to static function, compiler will inline it. See v3 patchset.

Signature · 1 line
Eugene