Skip to contentSkip to conversation

logically dead code in drivers/usb/gadget/core.c

U-Boot New Niv Shetrit 3 participants 4 messages Aug 27, 2019 USB

lore
Share & tools
lore
#0 lore raw

Fixed 'Logically dead code' coverity warning: in for loop,
stop condition is 'while descriptor==NULL' instead of 'while descriptor!=NULL'.

#1 lore raw

PATCH 1/1 removed '!' in for loop stop condition

---
 drivers/usb/gadget/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/core.c +1 -1
index ffaf161fb7..ae7387ee0a 100644
@@ -307,7 +307,7 @@ struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_devi
 	struct usb_endpoint_descriptor *endpoint_descriptor;
 	int i;
 
-	for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
+	for (i = 0; endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i); i++) {
 		if (endpoint_descriptor->bEndpointAddress == endpoint) {
 			return endpoint_descriptor;
 		}
Signature · 1 line
2.17.1
#2 lore raw

PATCH 1/1 removed '!' in for loop stop condition

Quoted 11 lines · Niv Shetrit
---
 drivers/usb/gadget/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/core.c +0 -0
index ffaf161fb7..ae7387ee0a 100644

drivers/usb/gadget/core.c struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_devi · line 313

Quoted 8 lines
drivers/usb/gadget/core.c +1 -1
@@ -307,7 +307,7 @@ struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_devi
 	struct usb_endpoint_descriptor *endpoint_descriptor;
 	int i;
 
-	for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
+	for (i = 0; endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i); i++) {
 		if (endpoint_descriptor->bEndpointAddress == endpoint) {
 			return endpoint_descriptor;
 		}

This changes the behavior of the code. Why is this needed ? The patch needs detailed explanation of and rationale behind the change.

#3 lore raw

PATCH 1/1 removed '!' in for loop stop condition

Quoted 14 lines · Marek Vasut
Quoted 13 lines · Niv Shetrit
---
 drivers/usb/gadget/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/core.c +0 -0
index ffaf161fb7..ae7387ee0a 100644
@@ -307,7 +307,7 @@ struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_devi
 	struct usb_endpoint_descriptor *endpoint_descriptor;
 	int i;
 
Quoted 9 lines

drivers/usb/gadget/core.c struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_devi · line 313

Quoted 6 lines
drivers/usb/gadget/core.c +1 -1
-	for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
+	for (i = 0; endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i); i++) {
 		if (endpoint_descriptor->bEndpointAddress == endpoint) {
 			return endpoint_descriptor;
 		}

This changes the behavior of the code. Why is this needed ? The patch needs detailed explanation of and rationale behind the change.

To start with, please put the coverity message you're addressing in the commit itself, not the introductory patch. Also please use Reported-by:
Coverity (CID: xxx) in the commit itself.

That said, not all issues coverity finds are strictly a problem and may be intentional or false positive. Coverity is complaining about how we're testing things here, but this rewrite doesn't have the same behavior. This type of issue is also a "code isn't as clear as it could be" thing because yes, it's uncommon to write the loop as it is here, but it's correct. Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190827/3bd2b280/attachment.sig>