Skip to contentSkip to conversation

staging: vt6655: use memset to make code clearer

PATCH Superseded Nam Cao 2 participants 4 messages Sep 9, 2022 Staging

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

PATCH v2 1/1 staging: vt6655: use memset to make code clearer

A line of code sets the entire struct vnt_rdes0 to zero by treating it as unsigned int. This works because sizeof(unsigned int) is equal to sizeof(struct vnt_rdes0) (4 bytes). However it is not obvious what this code is doing. Re-write this using memset to make the code clearer.

---
v2: re-write commit message because previous message describes a non-existent problem.

 drivers/staging/vt6655/device_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/vt6655/device_main.c +1 -1
index 8e2a976aaaad..a38657769c20 100644
@@ -867,7 +867,7 @@ static bool device_alloc_rx_buf(struct vnt_private *priv,
 		return false;
 	}
 
-	*((unsigned int *)&rd->rd0) = 0; /* FIX cast */
+	memset((void *)&rd->rd0, 0, sizeof(rd->rd0));
 
 	rd->rd0.res_count = cpu_to_le16(priv->rx_buf_sz);
 	rd->rd0.owner = OWNED_BY_NIC;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 18 lines · Nam Cao

A line of code sets the entire struct vnt_rdes0 to zero by treating it as unsigned int. This works because sizeof(unsigned int) is equal to sizeof(struct vnt_rdes0) (4 bytes). However it is not obvious what this code is doing. Re-write this using memset to make the code clearer.

---
v2: re-write commit message because previous message describes a non-existent problem.

 drivers/staging/vt6655/device_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/vt6655/device_main.c +0 -0
index 8e2a976aaaad..a38657769c20 100644

drivers/staging/vt6655/device_main.c static bool device_alloc_rx_buf(struct vnt_private *priv, · line 870

Quoted 5 lines
drivers/staging/vt6655/device_main.c +1 -1
@@ -867,7 +867,7 @@ static bool device_alloc_rx_buf(struct vnt_private *priv,
 		return false;
 	}
 
-	*((unsigned int *)&rd->rd0) = 0; /* FIX cast */
+	memset((void *)&rd->rd0, 0, sizeof(rd->rd0));

Why do you have to cast this to (void *)? That should almost never be needed.

thanks,

greg k-h

author
#2 lore raw
Quoted 2 lines · Greg KH

Why do you have to cast this to (void *)? That should almost never be needed.

Because I want to suppress a compiler warning:
drivers/staging/vt6655/device_main.c:869:9: warning: passing argument 1 of ‘memset’ discards ‘volatile’ qualifier from pointer target type [-Wdiscarded-qualifiers]

  869 |  memset(&rd->rd0, 0, sizeof(rd->rd0));
      |         ^~~~~~~~

Best regards,
Nam

maintainer
#3 lore raw
Quoted 10 lines · Nam Cao
Quoted 2 lines · Greg KH

Why do you have to cast this to (void *)? That should almost never be needed.

Because I want to suppress a compiler warning:
drivers/staging/vt6655/device_main.c:869:9: warning: passing argument 1 of ‘memset’ discards ‘volatile’ qualifier from pointer target type [-Wdiscarded-qualifiers]

  869 |  memset(&rd->rd0, 0, sizeof(rd->rd0));
      |         ^~~~~~~~

Why would that have a type of "volatile"? That should be fixed first please.

thanks,

greg k-h