Skip to contentSkip to conversation

staging: r8188eu: Fix different base types in assignments and parameters

PATCH In review Fabio M. De Francesco 2 participants 11 messages Jul 30, 2021 – Aug 4, 2021 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: r8188eu: Fix different base types in assignments and parameters

Fix sparse warnings of different base types in assignments and in passing function parameters.

---
 drivers/staging/r8188eu/core/rtw_br_ext.c | 46 ++++++++++++++++++-----
 1 file changed, 36 insertions(+), 10 deletions(-)
drivers/staging/r8188eu/core/rtw_br_ext.c +36 -10
index e00302137a60..31ca2e548555 100644
@@ -71,7 +71,7 @@ static inline int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *t
 	struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
 	int data_len;
 
-	data_len = tag->tag_len + TAG_HDR_LEN;
+	data_len = be16_to_cpu(tag->tag_len) + TAG_HDR_LEN;
 	if (skb_tailroom(skb) < data_len) {
 		_DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");
 		return -1;
@@ -134,42 +134,68 @@ static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr
 }
 
 static inline void __nat25_generate_ipx_network_addr_with_node(unsigned char *networkAddr,
-				unsigned int *ipxNetAddr, unsigned char *ipxNodeAddr)
+				__be32 *ipxNetAddr, unsigned char *ipxNodeAddr)
 {
+	union {
+                unsigned int f0;
+                unsigned char f1[IPX_NODE_LEN];
+        } addr;
+
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_IPX;
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);
 	memcpy(networkAddr+5, ipxNodeAddr, 6);
 }
 
 static inline void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,
-				unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
+				__be32 *ipxNetAddr, __be16 *ipxSocketAddr)
 {
+	union {
+		unsigned int f0;
+		unsigned char f1[4];
+	} addr;
+
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_IPX;
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
-	memcpy(networkAddr+5, (unsigned char *)ipxSocketAddr, 2);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);
+	addr.f0 ^= addr.f0;
+	addr.f0 = be16_to_cpu(*ipxSocketAddr);
+	memcpy(networkAddr+5, addr.f1, 2);
 }
 
 static inline void __nat25_generate_apple_network_addr(unsigned char *networkAddr,
-				unsigned short *network, unsigned char *node)
+				__be16 *network, unsigned char *node)
 {
+	union {
+                unsigned short f0;
+                unsigned char f1[2];
+        } addr;
+
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_APPLE;
-	memcpy(networkAddr+1, (unsigned char *)network, 2);
+	addr.f0 = be16_to_cpu(*network);
+	memcpy(networkAddr+1, addr.f1, 2);
 	networkAddr[3] = *node;
 }
 
 static inline void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
-				unsigned char *ac_mac, unsigned short *sid)
+				unsigned char *ac_mac, __be16 *sid)
 {
+	union {
+                unsigned short f0;
+                unsigned char f1[2];
+        } addr;
+
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_PPPOE;
-	memcpy(networkAddr+1, (unsigned char *)sid, 2);
+	addr.f0 = be16_to_cpu(*sid);
+	memcpy(networkAddr+1, addr.f1, 2);
 	memcpy(networkAddr+3, (unsigned char *)ac_mac, 6);
 }
 
Signature · 1 line
2.32.0
#1 lore raw
Quoted 2 lines · Fabio M. De Francesco

Fix sparse warnings of different base types in assignments and in passing function parameters.

This patch fixes some endian bugs but it's not mentioned at all in the commit message. Did you send to the correct patch?

Quoted 20 lines
---
 drivers/staging/r8188eu/core/rtw_br_ext.c | 46 ++++++++++++++++++-----
 1 file changed, 36 insertions(+), 10 deletions(-)
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -1
index e00302137a60..31ca2e548555 100644
@@ -71,7 +71,7 @@ static inline int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *t
 	struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
 	int data_len;
 
-	data_len = tag->tag_len + TAG_HDR_LEN;
+	data_len = be16_to_cpu(tag->tag_len) + TAG_HDR_LEN;
 	if (skb_tailroom(skb) < data_len) {
 		_DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");
 		return -1;

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 141

Quoted 9 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +4 -1
@@ -134,42 +134,68 @@ static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr
 }
 
 static inline void __nat25_generate_ipx_network_addr_with_node(unsigned char *networkAddr,
-				unsigned int *ipxNetAddr, unsigned char *ipxNodeAddr)
+				__be32 *ipxNetAddr, unsigned char *ipxNodeAddr)
 {
+	union {
+                unsigned int f0;
+                unsigned char f1[IPX_NODE_LEN];

What is going on here?? Why is f1 six bytes?

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 148

Quoted 8 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +4 -1
+        } addr;
+
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_IPX;
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);

What's the point of a union? memcpy() doesn't care about endian anotations.

Quoted 10 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +4 -1
 	memcpy(networkAddr+5, ipxNodeAddr, 6);
 }
 
 static inline void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,
-				unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
+				__be32 *ipxNetAddr, __be16 *ipxSocketAddr)
 {
+	union {
+		unsigned int f0;
+		unsigned char f1[4];

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 165

Quoted 10 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +5 -2
+	} addr;
+
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_IPX;
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
-	memcpy(networkAddr+5, (unsigned char *)ipxSocketAddr, 2);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);
+	addr.f0 ^= addr.f0;

What on earth????

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 166

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+	addr.f0 = be16_to_cpu(*ipxSocketAddr);

I'm so puzzled.

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 167

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+	memcpy(networkAddr+5, addr.f1, 2);

This patch is really weird so I'm done reviewing it.

regards,
dan carpenter

#2 lore raw
Quoted 7 lines · CEST Dan Carpenter
Quoted 2 lines · Fabio M. De Francesco

Fix sparse warnings of different base types in assignments and in passing function parameters.

This patch fixes some endian bugs but it's not mentioned at all in the commit message. Did you send to the correct patch?

Too late to change the commit message: Greg K-H has already taken this patch as-is (please see commit 56febcc2595e).

Quoted 8 lines
Quoted 8 lines

---

 drivers/staging/r8188eu/core/rtw_br_ext.c | 46 ++++++++++++++++++-----
 1 file changed, 36 insertions(+), 10 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/r8188eu/core/rtw_br_ext.c index

e00302137a60..31ca2e548555 100644

Quoted 4 lines
Quoted 4 lines

[...]

static inline void __nat25_generate_ipx_network_addr_with_node(unsigned

char

Quoted 2 lines

Review

Quoted 2 lines

*networkAddr,>

(quoted diff) +0 -1
-				unsigned int *ipxNetAddr, 

unsigned char *ipxNodeAddr)

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+				__be32 *ipxNetAddr, unsigned char 

*ipxNodeAddr)

Quoted 9 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 141

Quoted 6 lines

{

drivers/staging/r8188eu/core/rtw_br_ext.c +3 -0
+	union {
+                unsigned int f0;
+                unsigned char f1[IPX_NODE_LEN];

What is going on here?? Why is f1 six bytes?

Please look at the third parameter of the latest memcpy() in this function.

Quoted 5 lines
Quoted 5 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +2 -0
+        } addr;
+
 
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
Quoted 10 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 164

Quoted 5 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +2 -1
 	networkAddr[0] = NAT25_IPX;
 
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);

What's the point of a union? memcpy() doesn't care about endian anotations.

Quoted 1 line

memcpy(networkAddr+5, ipxNodeAddr, 6);

			                     ^^^^^

I'm talking about this memcpy().

Quoted 3 lines
Quoted 3 lines

}

static inline void __nat25_generate_ipx_network_addr_with_socket(unsigned

char

Quoted 2 lines

Review

Quoted 2 lines

*networkAddr,>

(quoted diff) +0 -1
-				unsigned int *ipxNetAddr, 

unsigned short *ipxSocketAddr)

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+				__be32 *ipxNetAddr, __be16 

*ipxSocketAddr)

Quoted 13 lines
Quoted 13 lines

{

drivers/staging/r8188eu/core/rtw_br_ext.c +5 -0
+	union {
+		unsigned int f0;
+		unsigned char f1[4];
+	} addr;
+
 
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_IPX;
Quoted 7 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 165

Quoted 5 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +3 -2
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
-	memcpy(networkAddr+5, (unsigned char *)ipxSocketAddr, 2);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);
+	addr.f0 ^= addr.f0;

What on earth????

I can't see any problem in xor(ing) a field with itself. Perhaps I read too much Assembly code :-) . However, am I missing something?

Quoted 7 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 166

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+	addr.f0 = be16_to_cpu(*ipxSocketAddr);

I'm so puzzled.

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 167

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+	memcpy(networkAddr+5, addr.f1, 2);

This patch is really weird so I'm done reviewing it.

I'm sorry that you don't like this patch, but Greg already had the last word on it.

Quoted 2 lines

regards,
dan carpenter

Regards,

Fabio

#3 lore raw
Quoted 7 lines · CEST Fabio M. De Francesco
Quoted 6 lines · CEST Dan Carpenter
Quoted 2 lines · Fabio M. De Francesco

Fix sparse warnings of different base types in assignments and in passing function parameters.

[...]

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 141

Quoted 3 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +3 -0
+	union {
+                unsigned int f0;
+                unsigned char f1[IPX_NODE_LEN];

What is going on here?? Why is f1 six bytes?

Please look at the third parameter of the latest memcpy() in this function.

No, I'm wrong here. I must have exchanged in my mind the latest and the memcpy() before the latest. So I see a '6' in the wrong memcpy().

I'll fix it ASAP.

Thanks,

Fabio

#4 lore raw
In reply to Dan Carpenter
Quoted 6 lines · CEST Dan Carpenter
Quoted 2 lines · Fabio M. De Francesco

Fix sparse warnings of different base types in assignments and in passing function parameters.

This patch fixes some endian bugs but it's not mentioned at all in the commit message. Did you send to the correct patch?

Dear Greg, Dan,

I just read another message where Greg K-H decided to revert this patch, probably also according to the review by Dan C..

In doing so, the sparse warnings of different base types in assignments and some endian bugs will appear anew and I think that these problem should be addressed in some other way.

I'll try to find some other solutions that result to be more correct and in line with Linux conventions. But, before doing the work I need to better understand what went wrong the first time. So, please, devote some time to respond to my questions...

Quoted 8 lines
Quoted 8 lines

---

 drivers/staging/r8188eu/core/rtw_br_ext.c | 46 ++++++++++++++++++-----
 1 file changed, 36 insertions(+), 10 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/r8188eu/core/rtw_br_ext.c index

e00302137a60..31ca2e548555 100644

Quoted 3 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline int __nat25_add_pppoe_tag(struct sk_buff

Quoted 3 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +0 -0
@@ -71,7 +71,7 @@ static inline int __nat25_add_pppoe_tag(struct sk_buff 

*skb, struct

Quoted 7 lines
Quoted 7 lines

pppoe_tag *t>

drivers/staging/r8188eu/core/rtw_br_ext.c +1 -1
 	struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
 	int data_len;
 
-	data_len = tag->tag_len + TAG_HDR_LEN;
+	data_len = be16_to_cpu(tag->tag_len) + TAG_HDR_LEN;
Quoted 6 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void

Quoted 6 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +0 -0
 	if (skb_tailroom(skb) < data_len) {
 
 		_DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");
 		return -1;
@@ -134,42 +134,68 @@ static inline void 

__nat25_generate_ipv4_network_addr(unsigned

Quoted 4 lines
Quoted 4 lines

char *networkAddr>
}

static inline void __nat25_generate_ipx_network_addr_with_node(unsigned

char

Quoted 2 lines

drivers/staging/r8188eu/core/rtw_br_ext.c

Quoted 2 lines

*networkAddr,>

drivers/staging/r8188eu/core/rtw_br_ext.c +0 -1
-				unsigned int *ipxNetAddr, 

unsigned char *ipxNodeAddr)

Quoted 1 line

drivers/staging/r8188eu/core/rtw_br_ext.c

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+				__be32 *ipxNetAddr, unsigned char 

*ipxNodeAddr)

Quoted 8 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 141

Quoted 6 lines

{

drivers/staging/r8188eu/core/rtw_br_ext.c +3 -0
+	union {
+                unsigned int f0;
+                unsigned char f1[IPX_NODE_LEN];

What is going on here?? Why is f1 six bytes?

Here I've been misled by something else. I agree that f1 should be four bytes.

Quoted 5 lines
Quoted 5 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +2 -0
+        } addr;
+
 
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
Quoted 8 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 164

Quoted 5 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +2 -1
 	networkAddr[0] = NAT25_IPX;
 
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);

What's the point of a union? memcpy() doesn't care about endian anotations.

I'm not sure to understand. Please, elaborate a bit. Do you mean that there's no need to use be32_to_cpu(*ipxNetAddr) and that we can use ipxNetAddr as-is (it is a __be32) as the second parameter of memcpy(), even though the networkAddr+1 is the address of an array of unsigned char? Is this that you tried to explain to me?

However, there's a huge error in passing the second parameter to memcpy(): it should be &addr.f1. Or better, let's make addr to be __be32 and then pass &addr (so there's no more need of that union). Do you agree?

Quoted 5 lines
Quoted 5 lines

memcpy(networkAddr+5, ipxNodeAddr, 6);

}

static inline void __nat25_generate_ipx_network_addr_with_socket(unsigned

char

Quoted 2 lines

drivers/staging/r8188eu/core/rtw_br_ext.c

Quoted 2 lines

*networkAddr,>

drivers/staging/r8188eu/core/rtw_br_ext.c +0 -1
-				unsigned int *ipxNetAddr, 

unsigned short *ipxSocketAddr)

Quoted 1 line

drivers/staging/r8188eu/core/rtw_br_ext.c

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+				__be32 *ipxNetAddr, __be16 

*ipxSocketAddr)

Quoted 13 lines
Quoted 13 lines

{

drivers/staging/r8188eu/core/rtw_br_ext.c +5 -0
+	union {
+		unsigned int f0;
+		unsigned char f1[4];
+	} addr;
+
 
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_IPX;
Quoted 7 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 165

Quoted 5 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +3 -2
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
-	memcpy(networkAddr+5, (unsigned char *)ipxSocketAddr, 2);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);
+	addr.f0 ^= addr.f0;

What on earth????

According to my understanding I need to wipe all the bits of addr.f0 before reusing it in the next assignment. Don't I have to?

If we agree on this, I'd like to explain why I use the xor of itself... I know that addr.f0 = 0; is more easily readable. But I've always thought that a direct assignment of a specific value (0, in this case) should indicate that one wants to use that 0 for something that needs that specific number.

For example, in for(i = 0; i < len; i++) to indicate that we want to loop starting from 0; or in res = some_func(); if( !res ) return _FAIL;, and so on. In this cases I want to make it clear that 0 has a precise and special meaning.

Differently, when one simply wants a variable wiped out of any content, just before re-using it for a subsequent "real" assignment, I prefer to xor(ing) that variable with itself.

Does it make sense to you?

Quoted 4 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 166

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+	addr.f0 = be16_to_cpu(*ipxSocketAddr);

I'm so puzzled.

Again like the assignment above, *ipxSocketAddr is of type __be16 and the destination of memcpy() is an array of unsigned char(s). I think I could just have a __be16 variable and pass the address of it to memcpy.

Do you agree with the solution above? (Probably I was so distracted that I didn't see I should have passed a pointer as the second parameter of memcpy()).

Quoted 6 lines

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 167

Quoted 1 line
drivers/staging/r8188eu/core/rtw_br_ext.c +1 -0
+	memcpy(networkAddr+5, addr.f1, 2);

This patch is really weird so I'm done reviewing it.

regards,
dan carpenter

I'm going to wait a bit for your reply to this message. If I don't receive any response I'll wait for the revert and make a new patch and see what happens. Or, I could fix the patch, if Greg does not revert 56febcc2595e.

Patiently waiting... :-)

Thanks,

Fabio

#5 lore raw

1) I'm pretty sure after a little more review that the patch introduces bugs. The data is supposed to be big endian.

2) The commit message did not clearly describe the "bug" and how the it looks like to the user.

3) The way we set variables to zero is we say "foo = 0;" We do not say "foo ^= foo;". Everyone knows the XOR a variable with itself trick but obfuscated code makes us cross.

4) The unions were unnecessary and ugly/bad.

regards,
dan carpenter

#6 lore raw
In reply to Fabio M. De Francesco
Quoted 9 lines · Fabio M. De Francesco

static inline void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,

drivers/staging/r8188eu/core/rtw_br_ext.c +6 -1
-				unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
+				__be32 *ipxNetAddr, __be16 *ipxSocketAddr)
 {
+	union {
+		unsigned int f0;
+		unsigned char f1[4];
+	} addr;
+

drivers/staging/r8188eu/core/rtw_br_ext.c static inline void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr · line 167

Quoted 10 lines
drivers/staging/r8188eu/core/rtw_br_ext.c +5 -2
 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
 
 	networkAddr[0] = NAT25_IPX;
-	memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
-	memcpy(networkAddr+5, (unsigned char *)ipxSocketAddr, 2);
+	addr.f0 = be32_to_cpu(*ipxNetAddr);
+	memcpy(networkAddr+1, addr.f1, 4);
+	addr.f0 ^= addr.f0;
+	addr.f0 = be16_to_cpu(*ipxSocketAddr);
+	memcpy(networkAddr+5, addr.f1, 2);

Here is another bug which was obscured/caused by the union.

	addr.f0 = be16_to_cpu(*ipxSocketAddr);

The addr.f0 variable is an int. On big endian systems only the last two bytes are set:

	memcpy(networkAddr+5, addr.f1, 2);

So this is the equivalent of:

	memset(networkAddr+5, 0, 2);

regards,
dan carpenter

#7 lore raw
Quoted 2 lines · CEST Dan Carpenter
Quoted 1 line · Fabio M. De Francesco

static inline void __nat25_generate_ipx_network_addr_with_socket(unsigned

char

Quoted 2 lines

Review

Quoted 2 lines

*networkAddr,>

(quoted diff) +0 -1
-				unsigned int *ipxNetAddr, 

unsigned short *ipxSocketAddr)

Quoted 10 lines
Quoted 3 lines

[...]

Here is another bug which was obscured/caused by the union.

	addr.f0 = be16_to_cpu(*ipxSocketAddr);

The addr.f0 variable is an int. On big endian systems only the last two bytes are set:

Quoted 8 lines
	memcpy(networkAddr+5, addr.f1, 2);

So this is the equivalent of:

	memset(networkAddr+5, 0, 2);

regards,
dan carpenter

Dear Dan,

Thanks, for pointing me to one more bug I introduced with this patch. The most of them were due to me forgetting that memcpy() takes pointers. For some reason I was thinking it takes values, therefore I put in it a lot of unnecessary and faulty complications.

I'd like to make a new patch, a better one (I hope), without unneeded unions without the other wrong lines that are in the commit 56febcc2595e.

However, I see that Greg hasn't yet had the time to revert the above commit, so I don't know how to make a new patch.

I mean: I could (1) either wait for Greg to revert it and then to fix the sparse warnings with a new patch, or (2) I could fix the bugs I made in 56febcc2595e without having it reverted. I would prefer the solution (2) with a "Fixes: 56febcc2595e (...)" and a "Reported-by: Dan Carpenter <...>" tags.

What is the best solution between the two above?

Thanks,

Fabio

#8 lore raw

The patch was based on the faulty premise that the original code was buggy so I don't think it can be fixed. It just needs to be reverted.

regards,
dan carpenter

#9 lore raw
Quoted 5 lines · CEST Dan Carpenter

The patch was based on the faulty premise that the original code was buggy so I don't think it can be fixed. It just needs to be reverted.

regards,
dan carpenter

With the original code, GCC + sparse emit a dozen of warnings like the following ones:

drivers/staging/r8188eu/core/rtw_br_ext.c:693:101: warning: incorrect type in argument 2 (different base types)
drivers/staging/r8188eu/core/rtw_br_ext.c:693:101: expected unsigned int
*ipxNetAddr
drivers/staging/r8188eu/core/rtw_br_ext.c:693:101: got restricted __be32 *
drivers/staging/r8188eu/core/rtw_br_ext.c:693:123: warning: incorrect type in argument 3 (different base types)
drivers/staging/r8188eu/core/rtw_br_ext.c:693:123: expected unsigned short
*ipxSocketAddr
drivers/staging/r8188eu/core/rtw_br_ext.c:693:123: got restricted __be16 *
drivers/staging/r8188eu/core/rtw_br_ext.c:698:99: warning: incorrect type in argument 2 (different base types)

Do we want the above pasted warnings and leave the code as-is?

Regards,

Fabio

#10 lore raw

There is no reason to assume that the Sparse annotations are correct or complete.

I'm sorry, but you're going to have to figure this one out on your own. I showed 100% that your patch introduced bugs. That's the extent of my job. I'm not going to answer any more questions.

regards,
dan carpenter