diff mbox series

[virt-pci] Fix application of sizeof to pointer

Message ID 20211012032333.184581-1-davidcomponentone@gmail.com
State Changes Requested
Headers show
Series [virt-pci] Fix application of sizeof to pointer | expand

Commit Message

Yang Guang Oct. 12, 2021, 3:23 a.m. UTC
From: David Yang <davidcomponentone@gmail.com>

The coccinelle check report:
"./arch/um/drivers/virt-pci.c:192:20-26:
ERROR: application of sizeof to pointer"
Using the "sizeof(buf->data)" to fix it.

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: David Yang <davidcomponentone@gmail.com>
---
 arch/um/drivers/virt-pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Joe Perches Oct. 12, 2021, 3:48 a.m. UTC | #1
On Tue, 2021-10-12 at 11:23 +0800, davidcomponentone@gmail.com wrote:
> From: David Yang <davidcomponentone@gmail.com>
> 
> The coccinelle check report:
> "./arch/um/drivers/virt-pci.c:192:20-26:
> ERROR: application of sizeof to pointer"
> Using the "sizeof(buf->data)" to fix it.
[]
> diff --git a/arch/um/drivers/virt-pci.c b/arch/um/drivers/virt-pci.c
[]
> @@ -189,7 +189,7 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
>  	buf = get_cpu_var(um_pci_msg_bufs);
>  	data = buf->data;
> 
> -	memset(data, 0xff, sizeof(data));
> +	memset(data, 0xff, sizeof(buf->data));

Perhaps change this to:

	memset(buf->data, 0xff, sizeof(buf->data));
	data = buf->data;

but honestly, the indirection to data doesn't make the code
much more readable so maybe just remove data altogether.
---
 arch/um/drivers/virt-pci.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/arch/um/drivers/virt-pci.c b/arch/um/drivers/virt-pci.c
index c080666330234..75e05ead97b9a 100644
--- a/arch/um/drivers/virt-pci.c
+++ b/arch/um/drivers/virt-pci.c
@@ -180,7 +180,6 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
 	};
 	/* buf->data is maximum size - we may only use parts of it */
 	struct um_pci_message_buffer *buf;
-	u8 *data;
 	unsigned long ret = ~0ULL;
 
 	if (!dev)
@@ -189,7 +188,7 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
 	buf = get_cpu_var(um_pci_msg_bufs);
 	data = buf->data;
 
-	memset(data, 0xff, sizeof(data));
+	memset(buf->data, 0xff, sizeof(buf->data));
 
 	switch (size) {
 	case 1:
@@ -204,22 +203,22 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
 		goto out;
 	}
 
-	if (um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, data, 8))
+	if (um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, buf->data, 8))
 		goto out;
 
 	switch (size) {
 	case 1:
-		ret = data[0];
+		ret = buf->data[0];
 		break;
 	case 2:
-		ret = le16_to_cpup((void *)data);
+		ret = le16_to_cpup((void *)buf->data);
 		break;
 	case 4:
-		ret = le32_to_cpup((void *)data);
+		ret = le32_to_cpup((void *)buf->data);
 		break;
 #ifdef CONFIG_64BIT
 	case 8:
-		ret = le64_to_cpup((void *)data);
+		ret = le64_to_cpup((void *)buf->data);
 		break;
 #endif
 	default:
Yang Guang Oct. 12, 2021, 6:03 a.m. UTC | #2
I'm agree with you. Using the "buf->data" instead is better.


On Mon, Oct 11, 2021 at 08:48:27PM -0700, Joe Perches wrote:
> On Tue, 2021-10-12 at 11:23 +0800, davidcomponentone@gmail.com wrote:
> > From: David Yang <davidcomponentone@gmail.com>
> >
> > The coccinelle check report:
> > "./arch/um/drivers/virt-pci.c:192:20-26:
> > ERROR: application of sizeof to pointer"
> > Using the "sizeof(buf->data)" to fix it.
> []
> > diff --git a/arch/um/drivers/virt-pci.c b/arch/um/drivers/virt-pci.c
> []
> > @@ -189,7 +189,7 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
> >     buf = get_cpu_var(um_pci_msg_bufs);
> >     data = buf->data;
> >
> > -   memset(data, 0xff, sizeof(data));
> > +   memset(data, 0xff, sizeof(buf->data));
>
> Perhaps change this to:
>
>       memset(buf->data, 0xff, sizeof(buf->data));
>       data = buf->data;
>
> but honestly, the indirection to data doesn't make the code
> much more readable so maybe just remove data altogether.
> ---
>  arch/um/drivers/virt-pci.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/arch/um/drivers/virt-pci.c b/arch/um/drivers/virt-pci.c
> index c080666330234..75e05ead97b9a 100644
> --- a/arch/um/drivers/virt-pci.c
> +++ b/arch/um/drivers/virt-pci.c
> @@ -180,7 +180,6 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
>       };
>       /* buf->data is maximum size - we may only use parts of it */
>       struct um_pci_message_buffer *buf;
> -     u8 *data;
>       unsigned long ret = ~0ULL;
>
>       if (!dev)
> @@ -189,7 +188,7 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
>       buf = get_cpu_var(um_pci_msg_bufs);
>       data = buf->data;
>
> -     memset(data, 0xff, sizeof(data));
> +     memset(buf->data, 0xff, sizeof(buf->data));
>
>       switch (size) {
>       case 1:
> @@ -204,22 +203,22 @@ static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
>               goto out;
>       }
>
> -     if (um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, data, 8))
> +     if (um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, buf->data, 8))
>               goto out;
>
>       switch (size) {
>       case 1:
> -             ret = data[0];
> +             ret = buf->data[0];
>               break;
>       case 2:
> -             ret = le16_to_cpup((void *)data);
> +             ret = le16_to_cpup((void *)buf->data);
>               break;
>       case 4:
> -             ret = le32_to_cpup((void *)data);
> +             ret = le32_to_cpup((void *)buf->data);
>               break;
>  #ifdef CONFIG_64BIT
>       case 8:
> -             ret = le64_to_cpup((void *)data);
> +             ret = le64_to_cpup((void *)buf->data);
>               break;
>  #endif
>       default:
>
>
diff mbox series

Patch

diff --git a/arch/um/drivers/virt-pci.c b/arch/um/drivers/virt-pci.c
index c08066633023..705275c5176c 100644
--- a/arch/um/drivers/virt-pci.c
+++ b/arch/um/drivers/virt-pci.c
@@ -189,7 +189,7 @@  static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
 	buf = get_cpu_var(um_pci_msg_bufs);
 	data = buf->data;
 
-	memset(data, 0xff, sizeof(data));
+	memset(data, 0xff, sizeof(buf->data));
 
 	switch (size) {
 	case 1: