linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer
@ 2021-12-07 13:53 Guo Zhengkui
  2021-12-07 14:40 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 7+ messages in thread
From: Guo Zhengkui @ 2021-12-07 13:53 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Alan Stern, Li Jun, Kishon Vijay Abraham I,
	Guo Zhengkui, Andrey Konovalov, Peter Chen,
	open list:USB SUBSYSTEM, open list
  Cc: kernel

Fix following error:
./drivers/usb/core/hcd.c:1284:38-44: ERROR:
application of sizeof to pointer.

Use sizeof(*vaddr) instead.

Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
---
 drivers/usb/core/hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 4d326ee12c36..996d5273cf60 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -1281,7 +1281,7 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
 		return -EFAULT;
 	}
 
-	vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
+	vaddr = hcd_buffer_alloc(bus, size + sizeof(*vaddr),
 				 mem_flags, dma_handle);
 	if (!vaddr)
 		return -ENOMEM;
-- 
2.20.1


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

* Re: [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer
  2021-12-07 13:53 [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer Guo Zhengkui
@ 2021-12-07 14:40 ` Greg Kroah-Hartman
  2021-12-07 22:21   ` Alan Stern
       [not found]   ` <AJkA6AAaE4s5AAqOmmsZjapb.9.1638915668969.Hmail.guozhengkui@vivo.com.@PFlhL2VVYmROMStBQkZWV2ZAcm93bGFuZC5oYXJ2YXJkLmVkdT4=>
  0 siblings, 2 replies; 7+ messages in thread
From: Greg Kroah-Hartman @ 2021-12-07 14:40 UTC (permalink / raw)
  To: Guo Zhengkui
  Cc: Alan Stern, Li Jun, Kishon Vijay Abraham I, Andrey Konovalov,
	Peter Chen, open list:USB SUBSYSTEM, open list, kernel

On Tue, Dec 07, 2021 at 09:53:47PM +0800, Guo Zhengkui wrote:
> Fix following error:
> ./drivers/usb/core/hcd.c:1284:38-44: ERROR:
> application of sizeof to pointer.

What generated this error?

> 
> Use sizeof(*vaddr) instead.
> 
> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
> ---
>  drivers/usb/core/hcd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 4d326ee12c36..996d5273cf60 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -1281,7 +1281,7 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
>  		return -EFAULT;
>  	}
>  
> -	vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
> +	vaddr = hcd_buffer_alloc(bus, size + sizeof(*vaddr),

I think you just broke the code.

Look at this closer and see what the function is doing with this buffer
and if you still think your patch is correct, please rewrite the
changelog text to explain why it is so (hint, just using the output of
coccinelle isn't ok.)

thanks,

greg k-h

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

* Re: [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer
  2021-12-07 14:40 ` Greg Kroah-Hartman
@ 2021-12-07 22:21   ` Alan Stern
  2021-12-08  5:43     ` Greg Kroah-Hartman
  2021-12-09  6:23     ` [PATCH] usb: core: hcd: change sizeof(vaddr) to sizeof(unsigned long) Guo Zhengkui
       [not found]   ` <AJkA6AAaE4s5AAqOmmsZjapb.9.1638915668969.Hmail.guozhengkui@vivo.com.@PFlhL2VVYmROMStBQkZWV2ZAcm93bGFuZC5oYXJ2YXJkLmVkdT4=>
  1 sibling, 2 replies; 7+ messages in thread
From: Alan Stern @ 2021-12-07 22:21 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Guo Zhengkui, Li Jun, Kishon Vijay Abraham I, Andrey Konovalov,
	Peter Chen, open list:USB SUBSYSTEM, open list, kernel

On Tue, Dec 07, 2021 at 03:40:37PM +0100, Greg Kroah-Hartman wrote:
> On Tue, Dec 07, 2021 at 09:53:47PM +0800, Guo Zhengkui wrote:
> > Fix following error:
> > ./drivers/usb/core/hcd.c:1284:38-44: ERROR:
> > application of sizeof to pointer.
> 
> What generated this error?
> 
> > 
> > Use sizeof(*vaddr) instead.
> > 
> > Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
> > ---
> >  drivers/usb/core/hcd.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> > index 4d326ee12c36..996d5273cf60 100644
> > --- a/drivers/usb/core/hcd.c
> > +++ b/drivers/usb/core/hcd.c
> > @@ -1281,7 +1281,7 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
> >  		return -EFAULT;
> >  	}
> >  
> > -	vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
> > +	vaddr = hcd_buffer_alloc(bus, size + sizeof(*vaddr),
> 
> I think you just broke the code.
> 
> Look at this closer and see what the function is doing with this buffer
> and if you still think your patch is correct, please rewrite the
> changelog text to explain why it is so (hint, just using the output of
> coccinelle isn't ok.)

Although the patch is definitely wrong, the code could stand to be 
improved.  The value stored at the end of the buffer is *vaddr_handle 
converted to an unsigned long, but the space reserved for this value is 
sizeof(vaddr) -- which doesn't make much sense since vaddr is a pointer 
to unsigned char.  The code implicitly relies on the fact that unsigned 
long takes up the same amount of space as a pointer.

Readers wouldn't have to stop and figure this out if the amount of 
reserved space was simply set to sizeof(unsigned long) rather than 
sizeof(vaddr).

Alan Stern

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

* Re: [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer
       [not found]   ` <AJkA6AAaE4s5AAqOmmsZjapb.9.1638915668969.Hmail.guozhengkui@vivo.com.@PFlhL2VVYmROMStBQkZWV2ZAcm93bGFuZC5oYXJ2YXJkLmVkdT4=>
@ 2021-12-08  3:00     ` Guo Zhengkui
  2021-12-08 21:36       ` Alan Stern
  0 siblings, 1 reply; 7+ messages in thread
From: Guo Zhengkui @ 2021-12-08  3:00 UTC (permalink / raw)
  To: Alan Stern, Greg Kroah-Hartman
  Cc: Li Jun, Kishon Vijay Abraham I, Andrey Konovalov, Peter Chen,
	open list:USB SUBSYSTEM, open list, kernel

On 2021/12/8 6:21, Alan Stern wrote:
> On Tue, Dec 07, 2021 at 03:40:37PM +0100, Greg Kroah-Hartman wrote:
>> On Tue, Dec 07, 2021 at 09:53:47PM +0800, Guo Zhengkui wrote:
>>> Fix following error:
>>> ./drivers/usb/core/hcd.c:1284:38-44: ERROR:
>>> application of sizeof to pointer.
>>
>> What generated this error?
>>
>>>
>>> Use sizeof(*vaddr) instead.
>>>
>>> Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
>>> ---
>>>   drivers/usb/core/hcd.c | 2 +-
>>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
>>> index 4d326ee12c36..996d5273cf60 100644
>>> --- a/drivers/usb/core/hcd.c
>>> +++ b/drivers/usb/core/hcd.c
>>> @@ -1281,7 +1281,7 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
>>>   		return -EFAULT;
>>>   	}
>>>   
>>> -	vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
>>> +	vaddr = hcd_buffer_alloc(bus, size + sizeof(*vaddr),
>>
>> I think you just broke the code.
>>
>> Look at this closer and see what the function is doing with this buffer
>> and if you still think your patch is correct, please rewrite the
>> changelog text to explain why it is so (hint, just using the output of
>> coccinelle isn't ok.)
> 

Sorry for my carelessness. It should be sizeof(vaddr).

> Although the patch is definitely wrong, the code could stand to be
> improved.  The value stored at the end of the buffer is *vaddr_handle
> converted to an unsigned long, but the space reserved for this value is
> sizeof(vaddr) -- which doesn't make much sense since vaddr is a pointer
> to unsigned char.  The code implicitly relies on the fact that unsigned
> long takes up the same amount of space as a pointer.
> 
> Readers wouldn't have to stop and figure this out if the amount of
> reserved space was simply set to sizeof(unsigned long) rather than
> sizeof(vaddr).

OK, I will commit another patch to fix this problem. Do you mind I add a 
"Suggested-by" tag of your name (Alan Stern) in this new patch?

Zhengkui

> 
> Alan Stern
> 

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

* Re: [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer
  2021-12-07 22:21   ` Alan Stern
@ 2021-12-08  5:43     ` Greg Kroah-Hartman
  2021-12-09  6:23     ` [PATCH] usb: core: hcd: change sizeof(vaddr) to sizeof(unsigned long) Guo Zhengkui
  1 sibling, 0 replies; 7+ messages in thread
From: Greg Kroah-Hartman @ 2021-12-08  5:43 UTC (permalink / raw)
  To: Alan Stern
  Cc: Guo Zhengkui, Li Jun, Kishon Vijay Abraham I, Andrey Konovalov,
	Peter Chen, open list:USB SUBSYSTEM, open list, kernel

On Tue, Dec 07, 2021 at 05:21:05PM -0500, Alan Stern wrote:
> On Tue, Dec 07, 2021 at 03:40:37PM +0100, Greg Kroah-Hartman wrote:
> > On Tue, Dec 07, 2021 at 09:53:47PM +0800, Guo Zhengkui wrote:
> > > Fix following error:
> > > ./drivers/usb/core/hcd.c:1284:38-44: ERROR:
> > > application of sizeof to pointer.
> > 
> > What generated this error?
> > 
> > > 
> > > Use sizeof(*vaddr) instead.
> > > 
> > > Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
> > > ---
> > >  drivers/usb/core/hcd.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> > > index 4d326ee12c36..996d5273cf60 100644
> > > --- a/drivers/usb/core/hcd.c
> > > +++ b/drivers/usb/core/hcd.c
> > > @@ -1281,7 +1281,7 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
> > >  		return -EFAULT;
> > >  	}
> > >  
> > > -	vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
> > > +	vaddr = hcd_buffer_alloc(bus, size + sizeof(*vaddr),
> > 
> > I think you just broke the code.
> > 
> > Look at this closer and see what the function is doing with this buffer
> > and if you still think your patch is correct, please rewrite the
> > changelog text to explain why it is so (hint, just using the output of
> > coccinelle isn't ok.)
> 
> Although the patch is definitely wrong, the code could stand to be 
> improved.  The value stored at the end of the buffer is *vaddr_handle 
> converted to an unsigned long, but the space reserved for this value is 
> sizeof(vaddr) -- which doesn't make much sense since vaddr is a pointer 
> to unsigned char.  The code implicitly relies on the fact that unsigned 
> long takes up the same amount of space as a pointer.

Linux requires that an unsigned long is the same size as a pointer, so
this is not a new requirement that is unique to this function :)

thanks,

greg k-h

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

* Re: [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer
  2021-12-08  3:00     ` [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer Guo Zhengkui
@ 2021-12-08 21:36       ` Alan Stern
  0 siblings, 0 replies; 7+ messages in thread
From: Alan Stern @ 2021-12-08 21:36 UTC (permalink / raw)
  To: Guo Zhengkui
  Cc: Greg Kroah-Hartman, Li Jun, Kishon Vijay Abraham I,
	Andrey Konovalov, Peter Chen, open list:USB SUBSYSTEM, open list,
	kernel

On Wed, Dec 08, 2021 at 11:00:41AM +0800, Guo Zhengkui wrote:
> On 2021/12/8 6:21, Alan Stern wrote:
> > On Tue, Dec 07, 2021 at 03:40:37PM +0100, Greg Kroah-Hartman wrote:
> > > On Tue, Dec 07, 2021 at 09:53:47PM +0800, Guo Zhengkui wrote:
> > > > Fix following error:
> > > > ./drivers/usb/core/hcd.c:1284:38-44: ERROR:
> > > > application of sizeof to pointer.
> > > 
> > > What generated this error?
> > > 
> > > > 
> > > > Use sizeof(*vaddr) instead.
> > > > 
> > > > Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
> > > > ---
> > > >   drivers/usb/core/hcd.c | 2 +-
> > > >   1 file changed, 1 insertion(+), 1 deletion(-)
> > > > 
> > > > diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> > > > index 4d326ee12c36..996d5273cf60 100644
> > > > --- a/drivers/usb/core/hcd.c
> > > > +++ b/drivers/usb/core/hcd.c
> > > > @@ -1281,7 +1281,7 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
> > > >   		return -EFAULT;
> > > >   	}
> > > > -	vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
> > > > +	vaddr = hcd_buffer_alloc(bus, size + sizeof(*vaddr),
> > > 
> > > I think you just broke the code.
> > > 
> > > Look at this closer and see what the function is doing with this buffer
> > > and if you still think your patch is correct, please rewrite the
> > > changelog text to explain why it is so (hint, just using the output of
> > > coccinelle isn't ok.)
> > 
> 
> Sorry for my carelessness. It should be sizeof(vaddr).
> 
> > Although the patch is definitely wrong, the code could stand to be
> > improved.  The value stored at the end of the buffer is *vaddr_handle
> > converted to an unsigned long, but the space reserved for this value is
> > sizeof(vaddr) -- which doesn't make much sense since vaddr is a pointer
> > to unsigned char.  The code implicitly relies on the fact that unsigned
> > long takes up the same amount of space as a pointer.
> > 
> > Readers wouldn't have to stop and figure this out if the amount of
> > reserved space was simply set to sizeof(unsigned long) rather than
> > sizeof(vaddr).
> 
> OK, I will commit another patch to fix this problem. Do you mind I add a
> "Suggested-by" tag of your name (Alan Stern) in this new patch?

That's fine.

Alan Stern

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

* [PATCH] usb: core: hcd: change sizeof(vaddr) to sizeof(unsigned long)
  2021-12-07 22:21   ` Alan Stern
  2021-12-08  5:43     ` Greg Kroah-Hartman
@ 2021-12-09  6:23     ` Guo Zhengkui
  1 sibling, 0 replies; 7+ messages in thread
From: Guo Zhengkui @ 2021-12-09  6:23 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Alan Stern, Kishon Vijay Abraham I, Li Jun,
	Sasha Levin, Guo Zhengkui, Sergey Shtylyov, Thinh Nguyen,
	Peter Chen, Andrey Konovalov, open list:USB SUBSYSTEM, open list
  Cc: kernel

`vaddr` is a pointer to unsigned char. sizeof(vaddr) here intends
to get the size of a pointer. But readers may get confused. Change
sizeof(vaddr) to sizeof(unsigned long) makes more sense.

Suggested-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
---
 drivers/usb/core/hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 4d326ee12c36..9ffc63ae65ac 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -1281,7 +1281,7 @@ static int hcd_alloc_coherent(struct usb_bus *bus,
 		return -EFAULT;
 	}
 
-	vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
+	vaddr = hcd_buffer_alloc(bus, size + sizeof(unsigned long),
 				 mem_flags, dma_handle);
 	if (!vaddr)
 		return -ENOMEM;
-- 
2.20.1


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

end of thread, other threads:[~2021-12-09  6:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-07 13:53 [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer Guo Zhengkui
2021-12-07 14:40 ` Greg Kroah-Hartman
2021-12-07 22:21   ` Alan Stern
2021-12-08  5:43     ` Greg Kroah-Hartman
2021-12-09  6:23     ` [PATCH] usb: core: hcd: change sizeof(vaddr) to sizeof(unsigned long) Guo Zhengkui
     [not found]   ` <AJkA6AAaE4s5AAqOmmsZjapb.9.1638915668969.Hmail.guozhengkui@vivo.com.@PFlhL2VVYmROMStBQkZWV2ZAcm93bGFuZC5oYXJ2YXJkLmVkdT4=>
2021-12-08  3:00     ` [PATCH] usb: core: hcd: fix bug: application of sizeof to pointer Guo Zhengkui
2021-12-08 21:36       ` Alan Stern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).