Re: [PATCH v2 4/5] staging: r8188eu: restricted __be16 degrades to int

From: Fabio M. De Francesco
Date: Fri Aug 20 2021 - 11:10:36 EST


On Thursday, August 19, 2021 7:20:44 PM CEST Greg Kroah-Hartman wrote:
> On Thu, Aug 19, 2021 at 01:47:56PM +0530, Aakash Hemadri wrote:
> > Fix sparse warning:
> > > rtw_br_ext.c:839:70: warning: restricted __be16 degrades to integer
> > > rtw_br_ext.c:845:70: warning: invalid assignment: |=
> > > rtw_br_ext.c:845:70: left side has type unsigned short
> > > rtw_br_ext.c:845:70: right side has type restricted __be16
> >
> > dhcp->flag is u16, remove htons() as __be16 degrades.
>
> Um, are you sure?
>
> >
> > Signed-off-by: Aakash Hemadri <aakashhemadri123@xxxxxxxxx>
> > ---
> > 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 d4acf02ca64f..14b2935cab98 100644
> > --- a/drivers/staging/r8188eu/core/rtw_br_ext.c
> > +++ b/drivers/staging/r8188eu/core/rtw_br_ext.c
> > @@ -674,13 +674,13 @@ void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
> > u32 cookie = dhcph->cookie;
> >
> > if (cookie == DHCP_MAGIC) { /* match magic word */
> > - if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
> > + if (!(dhcph->flags & BROADCAST_FLAG)) {
>
> So you now just ignore the fact that the code used to properly check
> BROADCAST_FLAG being in big endian mode, and now you assume it is native
> endian?
>
> Why is this ok? Did you test this?
>
> thanks,
>
> greg k-h
>
Aakash,

Building on the objections you had from Greg I suggest that, before attempting
anew to address problems like these, you get a better understanding of the topics of
native and network endianness and of the API that (conditionally) swap bytes
in a variable between little endian and big endian representation.

To start with, please note that the following code leads to tests for "v.vub[0] == 0xDD"
which is true on little endian architectures while "v.vub[0] == 0xAA" is true on big
endian ones...

union {
u32 vud;
u8 vub[4];
} v;

v.vud = 0xAABBCCDD;

Also note that API like cpu_to_be32(), htonl(), be32_to_cpu(), ntohl, and the likes are
used to (conditionally) swap bytes (i.e., change the arrangement of the bytes in a
multi-bytes variable).

Casts have very different purposes and usage patterns and, above all, they cannot
magically change the endianness of a variable.

Regards,

Fabio