Skip to contentSkip to conversation

usb: storage: initialize variable

PATCH Superseded [email protected] 2 participants 3 messages Aug 22, 2020 – Aug 24, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: storage: initialize variable

From: Tom Rix <[email protected]>

clang static analysis reports this representative problem

transport.c:495:15: warning: Assigned value is garbage or
undefined

        length_left -= partial;
                   ^  ~~~~~~~

partial is set only when usb_stor_bulk_transfer_sglist() is successful.

So initialize to partial to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/storage/transport.c +2 -2
index 238a8088e17f..ce920851b1f2 100644
@@ -461,7 +461,7 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
 		      struct scsi_cmnd* srb)
 {
-	unsigned int partial;
+	unsigned int partial = 0;
 	int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
 				      scsi_sg_count(srb), scsi_bufflen(srb),
 				      &partial);
@@ -484,7 +484,7 @@ int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
 		void *buf, unsigned int length_left, int use_sg, int *residual)
 {
 	int result;
-	unsigned int partial;
+	unsigned int partial = 0;
 
 	/* are we scatter-gathering? */
 	if (use_sg) {
Signature · 1 line
2.18.1
maintainer
#1 lore raw
Quoted 33 lines · On Sat, Aug 22, 2020 at 02:18:39PM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis reports this representative problem

transport.c:495:15: warning: Assigned value is garbage or
undefined

        length_left -= partial;
                   ^  ~~~~~~~

partial is set only when usb_stor_bulk_transfer_sglist() is successful.

So initialize to partial to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/storage/transport.c +1 -1
index 238a8088e17f..ce920851b1f2 100644
@@ -461,7 +461,7 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
 		      struct scsi_cmnd* srb)
 {
-	unsigned int partial;
+	unsigned int partial = 0;
 	int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
 				      scsi_sg_count(srb), scsi_bufflen(srb),
 				      &partial);

drivers/usb/storage/transport.c int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe, · line 490

Quoted 8 lines
drivers/usb/storage/transport.c +1 -1
@@ -484,7 +484,7 @@ int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
 		void *buf, unsigned int length_left, int use_sg, int *residual)
 {
 	int result;
-	unsigned int partial;
+	unsigned int partial = 0;
 
 	/* are we scatter-gathering? */
 	if (use_sg) {

Yes, this is a bug. But the right way to fix it is to change usb_stor_bulk_transfer_sglist(): Make it store 0 to *act_len at the start.

That way you change only one localized piece of code, instead of changing multiple callers and leaving a possibility of more errors being added in the future.

Alan Stern

author
#2 lore raw
Quoted 46 lines · Alan Stern
Quoted 33 lines · On Sat, Aug 22, 2020 at 02:18:39PM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis reports this representative problem

transport.c:495:15: warning: Assigned value is garbage or
undefined

        length_left -= partial;
                   ^  ~~~~~~~

partial is set only when usb_stor_bulk_transfer_sglist() is successful.

So initialize to partial to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/storage/transport.c +1 -1
index 238a8088e17f..ce920851b1f2 100644
@@ -461,7 +461,7 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
 		      struct scsi_cmnd* srb)
 {
-	unsigned int partial;
+	unsigned int partial = 0;
 	int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
 				      scsi_sg_count(srb), scsi_bufflen(srb),
 				      &partial);

drivers/usb/storage/transport.c int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe, · line 490

Quoted 8 lines
drivers/usb/storage/transport.c +1 -1
@@ -484,7 +484,7 @@ int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
 		void *buf, unsigned int length_left, int use_sg, int *residual)
 {
 	int result;
-	unsigned int partial;
+	unsigned int partial = 0;
 
 	/* are we scatter-gathering? */
 	if (use_sg) {

Yes, this is a bug. But the right way to fix it is to change usb_stor_bulk_transfer_sglist(): Make it store 0 to *act_len at the start.

Quoted 3 lines

That way you change only one localized piece of code, instead of changing multiple callers and leaving a possibility of more errors being added in the future.

I'll respin the patch.

It will exchange the compile time initialization fix for a run time if-check fix.

Quoted 2 lines

Alan Stern