Skip to contentSkip to conversation

usb: storage: suspicious code

PATCH In review Gustavo A. R. Silva 3 participants 6 messages Feb 15, 2017 – Feb 20, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello,

I ran into the following piece of code at drivers/usb/storage/jumpshot.c:305 (linux-next), and it seems a little bit suspicious:

// read the result. apparently the bulk write can complete // before the jumpshot drive is finished writing. so we loop // here until we get a good return code
waitcount = 0;

do {
	result = jumpshot_get_status(us);
	if (result != USB_STOR_TRANSPORT_GOOD) {
        	// I have not experimented to find the smallest value.
		//
		msleep(50);
	}
	} while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));

	if (result != USB_STOR_TRANSPORT_GOOD)
        	usb_stor_dbg(us, "Gah!  Waitcount = 10.  Bad write!?\n");

Variable 'waitcount' is never updated inside the do-while loop. So, either it isn't needed at all or line 316 should be modified (++waitcount < 10)

In case 'waitcount' isn't needed, lines 318 and 319 should be removed.

Can someone help me to clarify this so I can write a patch to fix this code?

Thank you
--
Gustavo A. R. Silva

#1 lore raw

Am Dienstag, den 14.02.2017, 23:06 -0600 schrieb Gustavo A. R. Silva:

Hi,

Quoted 12 lines

waitcount = 0;
do {
        result = jumpshot_get_status(us);
        if (result != USB_STOR_TRANSPORT_GOOD) {
                // I have not experimented to find the smallest value.
                //
                msleep(50);
        }
        } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));

Quoted 7 lines

        if (result != USB_STOR_TRANSPORT_GOOD) usb_stor_dbg(us, "Gah!  Waitcount = 10.  Bad write!?\n");

Variable 'waitcount' is never updated inside the do-while loop. So, either it isn't needed at all or line 316 should be modified (++waitcount < 10)

you are correct. Waitcount needs to be incremented.

HTH
Oliver

#2 lore raw

Hi Oliver,

Quoting Oliver Neukum <[email protected]>:

Quoted 20 lines

Am Dienstag, den 14.02.2017, 23:06 -0600 schrieb Gustavo A. R. Silva:

Hi,

Quoted 16 lines

waitcount = 0;
do {
        result = jumpshot_get_status(us);
        if (result != USB_STOR_TRANSPORT_GOOD) {
                // I have not experimented to find the smallest value.
                //
                msleep(50);
        }
        } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));

        if (result != USB_STOR_TRANSPORT_GOOD) usb_stor_dbg(us, "Gah!  Waitcount = 10.  Bad write!?\n");

Quoted 6 lines
Quoted 3 lines

Variable 'waitcount' is never updated inside the do-while loop. So, either it isn't needed at all or line 316 should be modified (++waitcount < 10)

you are correct. Waitcount needs to be incremented.

Thanks for clarifying, I'll send a patch shortly.

--
Gustavo A. R. Silva

#3 lore raw

PATCH 1/1 usb: storage: add missing pre-increment to variable

Add missing pre-increment to 'waitcount' variable used in do-while loop.

Addresses-Coverity-ID: 1011631

---
 drivers/usb/storage/jumpshot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/jumpshot.c +1 -1
index 011e527..a26c4bb 100644
@@ -313,7 +313,7 @@ static int jumpshot_write_data(struct us_data *us,
 				//
 				msleep(50); 
 			}
-		} while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
+		} while ((result != USB_STOR_TRANSPORT_GOOD) && (++waitcount < 10));
 
 		if (result != USB_STOR_TRANSPORT_GOOD)
 			usb_stor_dbg(us, "Gah!  Waitcount = 10.  Bad write!?\n");
Signature · 1 line
2.5.0
maintainer
#4 lore raw

Re: [usb-storage] [PATCH] usb: storage: add missing pre-increment to variable

Quoted 14 lines · Gustavo A. R. Silva

Add missing pre-increment to 'waitcount' variable used in do-while loop.

Addresses-Coverity-ID: 1011631

---
 drivers/usb/storage/jumpshot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/jumpshot.c +0 -0
index 011e527..a26c4bb 100644

drivers/usb/storage/jumpshot.c static int jumpshot_write_data(struct us_data *us, · line 319

Quoted 8 lines
drivers/usb/storage/jumpshot.c +1 -1
@@ -313,7 +313,7 @@ static int jumpshot_write_data(struct us_data *us,
 				//
 				msleep(50); 
 			}
-		} while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
+		} while ((result != USB_STOR_TRANSPORT_GOOD) && (++waitcount < 10));
 
 		if (result != USB_STOR_TRANSPORT_GOOD)
 			usb_stor_dbg(us, "Gah!  Waitcount = 10.  Bad write!?\n");

This has already been reported and fixed. See

	http://marc.info/?l=linux-usb&m=148604164024557&w=2

Alan Stern

#5 lore raw

Re: [usb-storage] [PATCH] usb: storage: add missing pre-increment to variable

Hi Alan,

Quoting Alan Stern <[email protected]>:

Quoted 21 lines · Gustavo A. R. Silva
Quoted 20 lines

Add missing pre-increment to 'waitcount' variable used in do-while loop.

Addresses-Coverity-ID: 1011631

---
 drivers/usb/storage/jumpshot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/jumpshot.c +1 -1
index 011e527..a26c4bb 100644
@@ -313,7 +313,7 @@ static int jumpshot_write_data(struct us_data *us,
 				//
 				msleep(50);
 			}
-		} while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
+		} while ((result != USB_STOR_TRANSPORT_GOOD) && (++waitcount < 10));
 
Quoted 8 lines

drivers/usb/storage/jumpshot.c static int jumpshot_write_data(struct us_data *us, · line 319

Quoted 3 lines
drivers/usb/storage/jumpshot.c +0 -0
 		if (result != USB_STOR_TRANSPORT_GOOD)
 			usb_stor_dbg(us, "Gah!  Waitcount = 10.  Bad write!?\n");

This has already been reported and fixed. See

	http://marc.info/?l=linux-usb&m=148604164024557&w=2

Awesome. Thanks for the info.
--
Gustavo A. R. Silva