All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] usb: storage: initialize variable
@ 2020-08-24 21:10 trix
  2020-08-24 21:18 ` Vito Caputo
  0 siblings, 1 reply; 5+ messages in thread
From: trix @ 2020-08-24 21:10 UTC (permalink / raw)
  To: stern, gregkh; +Cc: linux-usb, usb-storage, linux-kernel, Tom Rix

From: Tom Rix <trix@redhat.com>

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")
Signed-off-by: Tom Rix <trix@redhat.com>
---
 drivers/usb/storage/transport.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index 238a8088e17f..044429717dcc 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -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;
-- 
2.18.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] usb: storage: initialize variable
  2020-08-24 21:10 [PATCH v2] usb: storage: initialize variable trix
@ 2020-08-24 21:18 ` Vito Caputo
  2020-08-24 21:31   ` Tom Rix
  2020-08-25  0:35   ` Alan Stern
  0 siblings, 2 replies; 5+ messages in thread
From: Vito Caputo @ 2020-08-24 21:18 UTC (permalink / raw)
  To: trix; +Cc: stern, gregkh, linux-usb, usb-storage, linux-kernel

On Mon, Aug 24, 2020 at 02:10:27PM -0700, trix@redhat.com wrote:
> From: Tom Rix <trix@redhat.com>
> 
> 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")
> Signed-off-by: Tom Rix <trix@redhat.com>
> ---
>  drivers/usb/storage/transport.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
> index 238a8088e17f..044429717dcc 100644
> --- a/drivers/usb/storage/transport.c
> +++ b/drivers/usb/storage/transport.c
> @@ -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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] usb: storage: initialize variable
  2020-08-24 21:18 ` Vito Caputo
@ 2020-08-24 21:31   ` Tom Rix
  2020-08-25  0:36     ` Alan Stern
  2020-08-25  0:35   ` Alan Stern
  1 sibling, 1 reply; 5+ messages in thread
From: Tom Rix @ 2020-08-24 21:31 UTC (permalink / raw)
  To: Vito Caputo; +Cc: stern, gregkh, linux-usb, usb-storage, linux-kernel


On 8/24/20 2:18 PM, Vito Caputo wrote:
> On Mon, Aug 24, 2020 at 02:10:27PM -0700, trix@redhat.com wrote:
>> From: Tom Rix <trix@redhat.com>
>>
>> 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")
>> Signed-off-by: Tom Rix <trix@redhat.com>
>> ---
>>  drivers/usb/storage/transport.c | 3 +++
>>  1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
>> index 238a8088e17f..044429717dcc 100644
>> --- a/drivers/usb/storage/transport.c
>> +++ b/drivers/usb/storage/transport.c
>> @@ -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.

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

> Regards,
> Vito Caputo
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] usb: storage: initialize variable
  2020-08-24 21:18 ` Vito Caputo
  2020-08-24 21:31   ` Tom Rix
@ 2020-08-25  0:35   ` Alan Stern
  1 sibling, 0 replies; 5+ messages in thread
From: Alan Stern @ 2020-08-25  0:35 UTC (permalink / raw)
  To: Vito Caputo; +Cc: trix, gregkh, linux-usb, usb-storage, linux-kernel

On Mon, Aug 24, 2020 at 02:18:39PM -0700, Vito Caputo wrote:
> On Mon, Aug 24, 2020 at 02:10:27PM -0700, trix@redhat.com wrote:
> > From: Tom Rix <trix@redhat.com>
> > 
> > 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")
> > Signed-off-by: Tom Rix <trix@redhat.com>
> > ---
> >  drivers/usb/storage/transport.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
> > index 238a8088e17f..044429717dcc 100644
> > --- a/drivers/usb/storage/transport.c
> > +++ b/drivers/usb/storage/transport.c
> > @@ -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 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.

> 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] usb: storage: initialize variable
  2020-08-24 21:31   ` Tom Rix
@ 2020-08-25  0:36     ` Alan Stern
  0 siblings, 0 replies; 5+ messages in thread
From: Alan Stern @ 2020-08-25  0:36 UTC (permalink / raw)
  To: Tom Rix; +Cc: Vito Caputo, gregkh, linux-usb, usb-storage, linux-kernel

On Mon, Aug 24, 2020 at 02:31:01PM -0700, Tom Rix wrote:
> 
> On 8/24/20 2:18 PM, Vito Caputo wrote:
> > On Mon, Aug 24, 2020 at 02:10:27PM -0700, trix@redhat.com wrote:
> >> From: Tom Rix <trix@redhat.com>
> >>
> >> 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")
> >> Signed-off-by: Tom Rix <trix@redhat.com>
> >> ---
> >>  drivers/usb/storage/transport.c | 3 +++
> >>  1 file changed, 3 insertions(+)
> >>
> >> diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
> >> index 238a8088e17f..044429717dcc 100644
> >> --- a/drivers/usb/storage/transport.c
> >> +++ b/drivers/usb/storage/transport.c
> >> @@ -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.
> 
> 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-08-25  0:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-24 21:10 [PATCH v2] usb: storage: initialize variable trix
2020-08-24 21:18 ` Vito Caputo
2020-08-24 21:31   ` Tom Rix
2020-08-25  0:36     ` Alan Stern
2020-08-25  0:35   ` Alan Stern

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.