public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: core: Do not use sizeof on pointer type
@ 2016-04-22  5:51 Vaishali Thakkar
  2016-04-22  6:53 ` Clemens Ladisch
  2016-04-22  6:57 ` Bjørn Mork
  0 siblings, 2 replies; 3+ messages in thread
From: Vaishali Thakkar @ 2016-04-22  5:51 UTC (permalink / raw)
  To: gregkh; +Cc: linux-usb, linux-kernel, Vaishali Thakkar

When sizeof is applied to a pointer typed expression, it gives
the size of the pointer. So, do not use sizeof on pointer type.

Problem found using Coccinelle.

Signed-off-by: Vaishali Thakkar <vaishali.thakkar@oracle.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 2ca2cef..2aa352d 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -1386,7 +1386,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.1.4

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

* Re: [PATCH] usb: core: Do not use sizeof on pointer type
  2016-04-22  5:51 [PATCH] usb: core: Do not use sizeof on pointer type Vaishali Thakkar
@ 2016-04-22  6:53 ` Clemens Ladisch
  2016-04-22  6:57 ` Bjørn Mork
  1 sibling, 0 replies; 3+ messages in thread
From: Clemens Ladisch @ 2016-04-22  6:53 UTC (permalink / raw)
  To: Vaishali Thakkar; +Cc: gregkh, linux-usb, linux-kernel

Vaishali Thakkar wrote:
> When sizeof is applied to a pointer typed expression, it gives
> the size of the pointer.

And why would that be wrong in this case?

> +++ b/drivers/usb/core/hcd.c
> @@ -1386,7 +1386,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;
>

Please note the following comment:

	/*
	 * Store the virtual address of the buffer at the end
	 * of the allocated dma buffer. [...]


Regards,
Clemens

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

* Re: [PATCH] usb: core: Do not use sizeof on pointer type
  2016-04-22  5:51 [PATCH] usb: core: Do not use sizeof on pointer type Vaishali Thakkar
  2016-04-22  6:53 ` Clemens Ladisch
@ 2016-04-22  6:57 ` Bjørn Mork
  1 sibling, 0 replies; 3+ messages in thread
From: Bjørn Mork @ 2016-04-22  6:57 UTC (permalink / raw)
  To: Vaishali Thakkar; +Cc: gregkh, linux-usb, linux-kernel

Vaishali Thakkar <vaishali.thakkar@oracle.com> writes:

> When sizeof is applied to a pointer typed expression, it gives
> the size of the pointer. So, do not use sizeof on pointer type.

What if the intended result was the size of the pointer?

> Problem found using Coccinelle.

Yes, sure.  But you cannot just blindly apply the result without reading
and understanding the code.


Bjørn

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

end of thread, other threads:[~2016-04-22  6:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-22  5:51 [PATCH] usb: core: Do not use sizeof on pointer type Vaishali Thakkar
2016-04-22  6:53 ` Clemens Ladisch
2016-04-22  6:57 ` Bjørn Mork

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox