Skip to contentSkip to conversation

usb: storage: initialize variable

PATCH In review [email protected] 3 participants 5 messages Aug 24, 2020 – Aug 25, 2020

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

PATCH v2 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 set partial on entry to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/storage/transport.c +3 -0
index 238a8088e17f..044429717dcc 100644
@@ -414,6 +414,9 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 {
 	int result;
 
+	if (act_len)
+		*act_len = 0;
+
 	/* don't submit s-g requests during abort processing */
 	if (test_bit(US_FLIDX_ABORTING, &us->dflags))
 		return USB_STOR_XFER_ERROR;
Signature · 1 line
2.18.1
#1 lore raw
Quoted 24 lines · On Mon, Aug 24, 2020 at 02:10:27PM -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 set partial on entry to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/storage/transport.c +0 -0
index 238a8088e17f..044429717dcc 100644

drivers/usb/storage/transport.c static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe, · line 422

Quoted 9 lines
drivers/usb/storage/transport.c +3 -0
@@ -414,6 +414,9 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 {
 	int result;
 
+	if (act_len)
+		*act_len = 0;
+
 	/* don't submit s-g requests during abort processing */
 	if (test_bit(US_FLIDX_ABORTING, &us->dflags))
 		return USB_STOR_XFER_ERROR;

At a glance this seems odd to me. If the caller insists on ignoring the return value, shouldn't it just initialize partial to zero?

In my experience it's generally frowned upon for functions to store results in error paths.

Regards,
Vito Caputo

author
#2 lore raw
Quoted 37 lines · Vito Caputo
Quoted 24 lines · On Mon, Aug 24, 2020 at 02:10:27PM -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 set partial on entry to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/storage/transport.c +0 -0
index 238a8088e17f..044429717dcc 100644

drivers/usb/storage/transport.c static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe, · line 422

Quoted 9 lines
drivers/usb/storage/transport.c +3 -0
@@ -414,6 +414,9 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 {
 	int result;
 
+	if (act_len)
+		*act_len = 0;
+
 	/* don't submit s-g requests during abort processing */
 	if (test_bit(US_FLIDX_ABORTING, &us->dflags))
 		return USB_STOR_XFER_ERROR;

At a glance this seems odd to me. If the caller insists on ignoring the return value, shouldn't it just initialize partial to zero?

Quoted 2 lines

In my experience it's generally frowned upon for functions to store results in error paths.

Then maybe v1 is more appropriate.

Else i can spin a v3.

My preference is v1 as it doesn't add any runtime if-checks.

Tom

Quoted 3 lines

Regards,
Vito Caputo

maintainer
#3 lore raw
Quoted 39 lines · Tom Rix
Quoted 37 lines · Vito Caputo
Quoted 24 lines · On Mon, Aug 24, 2020 at 02:10:27PM -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 set partial on entry to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/storage/transport.c +0 -0
index 238a8088e17f..044429717dcc 100644

drivers/usb/storage/transport.c static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe, · line 422

Quoted 9 lines
drivers/usb/storage/transport.c +3 -0
@@ -414,6 +414,9 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 {
 	int result;
 
+	if (act_len)
+		*act_len = 0;
+
 	/* don't submit s-g requests during abort processing */
 	if (test_bit(US_FLIDX_ABORTING, &us->dflags))
 		return USB_STOR_XFER_ERROR;

At a glance this seems odd to me. If the caller insists on ignoring the return value, shouldn't it just initialize partial to zero?

Quoted 8 lines
Quoted 2 lines

In my experience it's generally frowned upon for functions to store results in error paths.

Then maybe v1 is more appropriate.

Else i can spin a v3.

My preference is v1 as it doesn't add any runtime if-checks.

If you really want to get rid of the runtime check (both the one you added and the one already present), you can audit all the callers of this routine to make certain that none of them pass a NULL pointer for act_len.

Alan Stern

maintainer
#4 lore raw
In reply to Vito Caputo
Quoted 28 lines · Vito Caputo
Quoted 27 lines · On Mon, Aug 24, 2020 at 02:10:27PM -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 set partial on entry to 0.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/storage/transport.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/storage/transport.c +0 -0
index 238a8088e17f..044429717dcc 100644
@@ -414,6 +414,9 @@ static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
 {
 	int result;
 
Quoted 9 lines

drivers/usb/storage/transport.c static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe, · line 422

Quoted 6 lines
drivers/usb/storage/transport.c +3 -0
+	if (act_len)
+		*act_len = 0;
+
 	/* don't submit s-g requests during abort processing */
 	if (test_bit(US_FLIDX_ABORTING, &us->dflags))
 		return USB_STOR_XFER_ERROR;

At a glance this seems odd to me. If the caller insists on ignoring the return value, shouldn't it just initialize partial to zero?

In this case, the callers are not the final consumers of the return value or of partial. They merely copy those values back up to _their_ callers, and those copy operations are what the static analyzer objects to.

Quoted 2 lines

In my experience it's generally frowned upon for functions to store results in error paths.

I don't see any reason for such an attitude, at least not here. It makes perfectly good sense, if an error prevents transmission of an entire data buffer, to store the amount of data that did get transmitted.

Alan Stern