Skip to contentSkip to conversation

staging: r8188eu: simplify complex pointer casting

PATCH Superseded Deepak R Varma 3 participants 4 messages Nov 6, 2022 Staging

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: r8188eu: simplify complex pointer casting

Pointers to structures udphdr and dhcpMessage are derived by casting adjacent pointers with size_t. Such typecast of pointer using size_t is not preferred. The code looks complex and delicate. Simplify such casting by utilizing generic "void *" casting.
While at this change, remove the unnecessary __be32 casting for member variable "cookie".

---
 drivers/staging/r8188eu/core/rtw_br_ext.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/core/rtw_br_ext.c +6 -4
index a23f7df373ed..e9b0906d0d74 100644
@@ -610,13 +610,15 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
 			struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
 
 			if (iph->protocol == IPPROTO_UDP) { /*  UDP */
-				struct udphdr *udph = (struct udphdr *)((size_t)iph + (iph->ihl << 2));
+				struct udphdr *udph = (void *)iph + (iph->ihl << 2);
 
 				if ((udph->source == htons(CLIENT_PORT)) &&
 				    (udph->dest == htons(SERVER_PORT))) { /*  DHCP request */
-					struct dhcpMessage *dhcph =
-						(struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));
-					u32 cookie = be32_to_cpu((__be32)dhcph->cookie);
+					u32 cookie;
+					struct dhcpMessage *dhcph;
+
+					dhcph = (void *)udph + sizeof(struct udphdr);
+					cookie = be32_to_cpu(dhcph->cookie);
 
 					if (cookie == DHCP_MAGIC) { /*  match magic word */
 						if (!(dhcph->flags & htons(BROADCAST_FLAG))) {

--
2.34.1

Tested-by on v1 1/1
#1 lore raw
Quoted 38 lines · Deepak R Varma

Pointers to structures udphdr and dhcpMessage are derived by casting adjacent pointers with size_t. Such typecast of pointer using size_t is not preferred. The code looks complex and delicate. Simplify such casting by utilizing generic "void *" casting.
While at this change, remove the unnecessary __be32 casting for member variable "cookie".

---
  drivers/staging/r8188eu/core/rtw_br_ext.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/core/rtw_br_ext.c +6 -5
index a23f7df373ed..e9b0906d0d74 100644
@@ -610,13 +610,15 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
  			struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
 
  			if (iph->protocol == IPPROTO_UDP) { /*  UDP */
-				struct udphdr *udph = (struct udphdr *)((size_t)iph + (iph->ihl << 2));
+				struct udphdr *udph = (void *)iph + (iph->ihl << 2);
 
  				if ((udph->source == htons(CLIENT_PORT)) &&
  				    (udph->dest == htons(SERVER_PORT))) { /*  DHCP request */
-					struct dhcpMessage *dhcph =
-						(struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));
-					u32 cookie = be32_to_cpu((__be32)dhcph->cookie);
+					u32 cookie;
+					struct dhcpMessage *dhcph;
+
+					dhcph = (void *)udph + sizeof(struct udphdr);
+					cookie = be32_to_cpu(dhcph->cookie);
 
  					if (cookie == DHCP_MAGIC) { /*  match magic word */
  						if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
--
Quoted 5 lines

2.34.1

#2 lore raw
In reply to Deepak R Varma
Quoted 6 lines · Deepak R Varma

Pointers to structures udphdr and dhcpMessage are derived by casting adjacent pointers with size_t. Such typecast of pointer using size_t is not preferred. The code looks complex and delicate. Simplify such casting by utilizing generic "void *" casting.
While at this change, remove the unnecessary __be32 casting for member variable "cookie".

Wait, why is that unecessary?

And why is that part of this change, that should be a separate one, so that it can be reverted when it's found to be buggy :)

Quoted 15 lines
---
 drivers/staging/r8188eu/core/rtw_br_ext.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/core/rtw_br_ext.c +0 -0
index a23f7df373ed..e9b0906d0d74 100644
@@ -610,13 +610,15 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
 			struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
 
 			if (iph->protocol == IPPROTO_UDP) { /*  UDP */

drivers/staging/r8188eu/core/rtw_br_ext.c void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb) · line 618

Quoted 10 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +3 -4
-				struct udphdr *udph = (struct udphdr *)((size_t)iph + (iph->ihl << 2));
+				struct udphdr *udph = (void *)iph + (iph->ihl << 2);
 
 				if ((udph->source == htons(CLIENT_PORT)) &&
 				    (udph->dest == htons(SERVER_PORT))) { /*  DHCP request */
-					struct dhcpMessage *dhcph =
-						(struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));
-					u32 cookie = be32_to_cpu((__be32)dhcph->cookie);
+					u32 cookie;
+					struct dhcpMessage *dhcph;

Why reorder these variables?

drivers/staging/r8188eu/core/rtw_br_ext.c void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb) · line 621

Quoted 3 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +3 -0
+
+					dhcph = (void *)udph + sizeof(struct udphdr);
+					cookie = be32_to_cpu(dhcph->cookie);

The cookie change should be separate please.

thanks,

greg k-h

#3 lore raw
Quoted 9 lines · Greg Kroah-Hartman
Quoted 6 lines · Deepak R Varma

Pointers to structures udphdr and dhcpMessage are derived by casting adjacent pointers with size_t. Such typecast of pointer using size_t is not preferred. The code looks complex and delicate. Simplify such casting by utilizing generic "void *" casting.
While at this change, remove the unnecessary __be32 casting for member variable "cookie".

Wait, why is that unecessary?

Hello Greg,
cookie is already defined to be a __be32 type as part of the dhcpMesssade structure declared in the same file.

15 struct dhcpMessage {

 14         u_int8_t op;
  1         u_int8_t file[128];
597         __be32 cookie;
  1         u_int8_t options[308]; /* 312 - cookie */

2 };
3

Quoted 3 lines

And why is that part of this change, that should be a separate one, so that it can be reverted when it's found to be buggy :)

Sounds good. I will submit that as a separate patch.

Quoted 19 lines
Quoted 19 lines
---
 drivers/staging/r8188eu/core/rtw_br_ext.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -1
index a23f7df373ed..e9b0906d0d74 100644
@@ -610,13 +610,15 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
 			struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
 
 			if (iph->protocol == IPPROTO_UDP) { /*  UDP */
-				struct udphdr *udph = (struct udphdr *)((size_t)iph + (iph->ihl << 2));
+				struct udphdr *udph = (void *)iph + (iph->ihl << 2);
 
Quoted 9 lines

drivers/staging/r8188eu/core/rtw_br_ext.c void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb) · line 618

Quoted 7 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +2 -3
 				if ((udph->source == htons(CLIENT_PORT)) &&
 				    (udph->dest == htons(SERVER_PORT))) { /*  DHCP request */
-					struct dhcpMessage *dhcph =
-						(struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));
-					u32 cookie = be32_to_cpu((__be32)dhcph->cookie);
+					u32 cookie;
+					struct dhcpMessage *dhcph;

Why reorder these variables?

No specific reason. Since next instruction is associated with *dhcph, I thought it will simpler to follow. I will revise and resend.

Quoted 6 lines

drivers/staging/r8188eu/core/rtw_br_ext.c void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb) · line 621

Quoted 4 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +3 -0
+
+					dhcph = (void *)udph + sizeof(struct udphdr);
+					cookie = be32_to_cpu(dhcph->cookie);

The cookie change should be separate please.

Sounds good. Will split the patch into a patch set and resubmit.

Quoted 5 lines

thanks,

greg k-h