2021-08-21 16:20:26

by Aakash Hemadri

[permalink] [raw]
Subject: [PATCH v3 2/5] staging: r8188eu: cast to restricted __be32

Fix sparse warning:
> rtw_br_ext.c:836:54: warning: cast to restricted __be32

dhpch->cookie is be32, change it's type.

Suggested-by: Larry Finger <[email protected]>
Signed-off-by: Aakash Hemadri <[email protected]>
---
drivers/staging/r8188eu/core/rtw_br_ext.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/r8188eu/core/rtw_br_ext.c b/drivers/staging/r8188eu/core/rtw_br_ext.c
index f6d1f6029ec3..f65d94bfa286 100644
--- a/drivers/staging/r8188eu/core/rtw_br_ext.c
+++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
@@ -649,7 +649,7 @@ struct dhcpMessage {
u_int8_t chaddr[16];
u_int8_t sname[64];
u_int8_t file[128];
- u_int32_t cookie;
+ __be32 cookie;
u_int8_t options[308]; /* 312 - cookie */
};

@@ -671,7 +671,7 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
(udph->dest == __constant_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 = be32_to_cpu(dhcph->cookie);

if (cookie == DHCP_MAGIC) { /* match magic word */
if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
--
2.32.0


2021-08-23 08:46:10

by Aakash Hemadri

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] staging: r8188eu: cast to restricted __be32

On 21/08/21 09:48PM, Aakash Hemadri wrote:
> Fix sparse warning:
> > rtw_br_ext.c:836:54: warning: cast to restricted __be32
>
> dhpch->cookie is be32, change it's type.
>
> Suggested-by: Larry Finger <[email protected]>
> Signed-off-by: Aakash Hemadri <[email protected]>
> ---
> drivers/staging/r8188eu/core/rtw_br_ext.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/staging/r8188eu/core/rtw_br_ext.c b/drivers/staging/r8188eu/core/rtw_br_ext.c
> index f6d1f6029ec3..f65d94bfa286 100644
> --- a/drivers/staging/r8188eu/core/rtw_br_ext.c
> +++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
> @@ -649,7 +649,7 @@ struct dhcpMessage {
> u_int8_t chaddr[16];
> u_int8_t sname[64];
> u_int8_t file[128];
> - u_int32_t cookie;
> + __be32 cookie;
> u_int8_t options[308]; /* 312 - cookie */
> };
>
> @@ -671,7 +671,7 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
> (udph->dest == __constant_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 = be32_to_cpu(dhcph->cookie);
>
> if (cookie == DHCP_MAGIC) { /* match magic word */
> if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
> --
> 2.32.0
>

David Laight suggested to use get_unaligned_be32, I am not sure if it's
the right thing to do because as far as I understand get_unaligned_be32
byteshifts the argument.

Can someone please confirm if this change is okay?

Thanks,
Aakash Hemadri.

2021-08-23 14:27:24

by Larry Finger

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] staging: r8188eu: cast to restricted __be32

On 8/23/21 3:44 AM, Aakash Hemadri wrote:
> On 21/08/21 09:48PM, Aakash Hemadri wrote:
>> Fix sparse warning:
>>> rtw_br_ext.c:836:54: warning: cast to restricted __be32
>>
>> dhpch->cookie is be32, change it's type.
>>
>> Suggested-by: Larry Finger <[email protected]>
>> Signed-off-by: Aakash Hemadri <[email protected]>
>> ---
>> drivers/staging/r8188eu/core/rtw_br_ext.c | 4 ++--
>> 1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/staging/r8188eu/core/rtw_br_ext.c b/drivers/staging/r8188eu/core/rtw_br_ext.c
>> index f6d1f6029ec3..f65d94bfa286 100644
>> --- a/drivers/staging/r8188eu/core/rtw_br_ext.c
>> +++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
>> @@ -649,7 +649,7 @@ struct dhcpMessage {
>> u_int8_t chaddr[16];
>> u_int8_t sname[64];
>> u_int8_t file[128];
>> - u_int32_t cookie;
>> + __be32 cookie;
>> u_int8_t options[308]; /* 312 - cookie */
>> };
>>
>> @@ -671,7 +671,7 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
>> (udph->dest == __constant_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 = be32_to_cpu(dhcph->cookie);
>>
>> if (cookie == DHCP_MAGIC) { /* match magic word */
>> if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
>> --
>> 2.32.0
>>
>
> David Laight suggested to use get_unaligned_be32, I am not sure if it's
> the right thing to do because as far as I understand get_unaligned_be32
> byteshifts the argument.
>
> Can someone please confirm if this change is okay?
>

It is not needed. variable dhcph->cookie is 4-byte aligned. Usind the unaligned
version would just add cpu cycles and arrivw at the same point!

Larry

2021-08-25 09:15:49

by David Laight

[permalink] [raw]
Subject: RE: [PATCH v3 2/5] staging: r8188eu: cast to restricted __be32

From: Larry Finger
> Sent: 23 August 2021 15:24
>
> On 8/23/21 3:44 AM, Aakash Hemadri wrote:
> > On 21/08/21 09:48PM, Aakash Hemadri wrote:
> >> Fix sparse warning:
> >>> rtw_br_ext.c:836:54: warning: cast to restricted __be32
> >>
> >> dhpch->cookie is be32, change it's type.
...
> >> diff --git a/drivers/staging/r8188eu/core/rtw_br_ext.c b/drivers/staging/r8188eu/core/rtw_br_ext.c
> >> index f6d1f6029ec3..f65d94bfa286 100644
> >> --- a/drivers/staging/r8188eu/core/rtw_br_ext.c
> >> +++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
...
> >> @@ -671,7 +671,7 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
> >> (udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */
> >> struct dhcpMessage *dhcph =
> >> (struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));

This assignment looks dubious at best - WTF is (size_t) for?
I don't seem to have a source tree with this driver in it (probably only in 'next'?).
I suspect it should be:
struct dhcp_message *dhcph = (void *)(udph + 1);

> >> - u32 cookie = be32_to_cpu((__be32)dhcph->cookie);
> >> + u32 cookie = be32_to_cpu(dhcph->cookie);
> >>
> >> if (cookie == DHCP_MAGIC) { /* match magic word */
> >> if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
> >> --
> >> 2.32.0
> >>
> >
> > David Laight suggested to use get_unaligned_be32, I am not sure if it's
> > the right thing to do because as far as I understand get_unaligned_be32
> > byteshifts the argument.
> >
> > Can someone please confirm if this change is okay?
> >
>
> It is not needed. variable dhcph->cookie is 4-byte aligned. Usind the unaligned
> version would just add cpu cycles and arrivw at the same point!

It rather depends on whether the input buffer is aligned.
Although if it might not be then then the structure(s) that map
it need to be marked 'packed'.
Which would also mean the get_unaligned_be32() isn't the correct
way to handle a misaligned buffer.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2021-08-25 09:22:05

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] staging: r8188eu: cast to restricted __be32

On Wed, Aug 25, 2021 at 09:13:44AM +0000, David Laight wrote:
> From: Larry Finger
> > Sent: 23 August 2021 15:24
> >
> > On 8/23/21 3:44 AM, Aakash Hemadri wrote:
> > > On 21/08/21 09:48PM, Aakash Hemadri wrote:
> > >> Fix sparse warning:
> > >>> rtw_br_ext.c:836:54: warning: cast to restricted __be32
> > >>
> > >> dhpch->cookie is be32, change it's type.
> ...
> > >> diff --git a/drivers/staging/r8188eu/core/rtw_br_ext.c b/drivers/staging/r8188eu/core/rtw_br_ext.c
> > >> index f6d1f6029ec3..f65d94bfa286 100644
> > >> --- a/drivers/staging/r8188eu/core/rtw_br_ext.c
> > >> +++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
> ...
> > >> @@ -671,7 +671,7 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
> > >> (udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */
> > >> struct dhcpMessage *dhcph =
> > >> (struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));
>
> This assignment looks dubious at best - WTF is (size_t) for?
> I don't seem to have a source tree with this driver in it (probably only in 'next'?).
> I suspect it should be:
> struct dhcp_message *dhcph = (void *)(udph + 1);

Those are equivalent. Either way works.

regards,
dan carpenter

2021-08-25 10:26:43

by David Laight

[permalink] [raw]
Subject: RE: [PATCH v3 2/5] staging: r8188eu: cast to restricted __be32

From: Dan Carpenter
> Sent: 25 August 2021 10:19
>
> On Wed, Aug 25, 2021 at 09:13:44AM +0000, David Laight wrote:
> > From: Larry Finger
> > > Sent: 23 August 2021 15:24
> > >
> > > On 8/23/21 3:44 AM, Aakash Hemadri wrote:
> > > > On 21/08/21 09:48PM, Aakash Hemadri wrote:
> > > >> Fix sparse warning:
> > > >>> rtw_br_ext.c:836:54: warning: cast to restricted __be32
> > > >>
> > > >> dhpch->cookie is be32, change it's type.
> > ...
> > > >> diff --git a/drivers/staging/r8188eu/core/rtw_br_ext.c
> b/drivers/staging/r8188eu/core/rtw_br_ext.c
> > > >> index f6d1f6029ec3..f65d94bfa286 100644
> > > >> --- a/drivers/staging/r8188eu/core/rtw_br_ext.c
> > > >> +++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
> > ...
> > > >> @@ -671,7 +671,7 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
> > > >> (udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */
> > > >> struct dhcpMessage *dhcph =
> > > >> (struct dhcpMessage *)((size_t)udph + sizeof(struct
> udphdr));
> >
> > This assignment looks dubious at best - WTF is (size_t) for?
> > I don't seem to have a source tree with this driver in it (probably only in 'next'?).
> > I suspect it should be:
> > struct dhcp_message *dhcph = (void *)(udph + 1);
>
> Those are equivalent. Either way works.

size_t isn't guaranteed to be large enough to hold a pointer.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2021-08-25 10:42:16

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] staging: r8188eu: cast to restricted __be32

On Wed, Aug 25, 2021 at 10:21:23AM +0000, David Laight wrote:
> From: Dan Carpenter
> > Sent: 25 August 2021 10:19
> >
> > On Wed, Aug 25, 2021 at 09:13:44AM +0000, David Laight wrote:
> > > From: Larry Finger
> > > > Sent: 23 August 2021 15:24
> > > >
> > > > On 8/23/21 3:44 AM, Aakash Hemadri wrote:
> > > > > On 21/08/21 09:48PM, Aakash Hemadri wrote:
> > > > >> Fix sparse warning:
> > > > >>> rtw_br_ext.c:836:54: warning: cast to restricted __be32
> > > > >>
> > > > >> dhpch->cookie is be32, change it's type.
> > > ...
> > > > >> diff --git a/drivers/staging/r8188eu/core/rtw_br_ext.c
> > b/drivers/staging/r8188eu/core/rtw_br_ext.c
> > > > >> index f6d1f6029ec3..f65d94bfa286 100644
> > > > >> --- a/drivers/staging/r8188eu/core/rtw_br_ext.c
> > > > >> +++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
> > > ...
> > > > >> @@ -671,7 +671,7 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
> > > > >> (udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */
> > > > >> struct dhcpMessage *dhcph =
> > > > >> (struct dhcpMessage *)((size_t)udph + sizeof(struct
> > udphdr));
> > >
> > > This assignment looks dubious at best - WTF is (size_t) for?
> > > I don't seem to have a source tree with this driver in it (probably only in 'next'?).
> > > I suspect it should be:
> > > struct dhcp_message *dhcph = (void *)(udph + 1);
> >
> > Those are equivalent. Either way works.
>
> size_t isn't guaranteed to be large enough to hold a pointer.

It is on every system that Linux is ever going to build on.

regards,
dan carpenter