From: Julia Lawall <julia.lawall@lip6.fr>
To: Joe Perches <joe@perches.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>,
Frans Klaver <fransklaver@gmail.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
kernel-janitors <kernel-janitors@vger.kernel.org>,
Guenter Roeck <linux@roeck-us.net>,
Yueyao Zhu <yueyao.zhu@gmail.com>,
Rui Miguel Silva <rmfrfs@gmail.com>,
Guru Das Srinagesh <gurooodas@gmail.com>,
Javier Martinez Canillas <javier@dowhile0.org>,
devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org
Subject: Re: endian bitshift defects [ was: staging: fusb302: don't bitshift __le16 type ]
Date: Sat, 17 Jun 2017 06:00:38 +0000 [thread overview]
Message-ID: <alpine.DEB.2.20.1706170753570.2045@hadrien> (raw)
In-Reply-To: <1497678601.10546.32.camel@perches.com>
[-- Attachment #1: Type: text/plain, Size: 2309 bytes --]
On Fri, 16 Jun 2017, Joe Perches wrote:
> On Sat, 2017-06-17 at 07:23 +0200, Julia Lawall wrote:
> > On Fri, 16 Jun 2017, Joe Perches wrote:
> > > On Fri, 2017-06-16 at 19:45 +0200, Frans Klaver wrote:
> > > > The header field in struct pd_message is declared as an __le16 type. The
> > > > data in the message is supposed to be little endian. This means we don't
> > > > have to go and shift the individual bytes into position when we're
> > > > filling the buffer, we can just copy the contents right away. As an
> > > > added benefit we don't get fishy results on big endian systems anymore.
> > >
> > > Thanks for pointing this out.
> > >
> > > There are several instances of this class of error.
> > >
> > > Here's a cocci script to find them.
> > >
> > > This is best used with cocci's --all-includes option like:
> > >
> > > $ spatch --all-includes --very-quiet --sp-file lebe_bitshifts.cocci .
> > > [ many defects...]
>
> Probably would have been better as [ many possible defects... ]
OK
> > > $ cat lebe_bitshifts.cocci
> > > @@
> > > typedef __le16, __le32, __le64, __be16, __be32, __be64;
> > > { __le16, __le32, __le64, __be16, __be32, __be64 } a;
> > > expression b;
> > > @@
> > >
> > > * a << b
>
> [etc...]
>
> > Is this always a problem?
>
> No, not always.
>
> If the CPU is the equivalent endian, the bitshift is fine.
> It can't be known if the code is only compiled on a
> single cpu type. It is rather odd though to use endian
> notation if the code is compiled for a single cpu type.
Is there some way to know from the code if it is compiled for a single cou
type?
> > Would it be useful to add this to the scripts
> > in the kernel?
>
> Maybe.
If there are a lot of false positives, it could be a nuisance...
> btw: is there a way for the operators to be surrounded by
> some \( \| \) or some other bracket style so it could
> be written with a single test?
>
> Something like:
>
> @@
> typedef __le16, __le32, __le64, __be16, __be32, __be64;
> { __le16, __le32, __le64, __be16, __be32, __be64 } a;
> expression b;
> @@
>
> * a [<<|<<=|>>|>>=] b
Partly. You can define
binary operator bop = {<<,>>};
or
assignment operator aop = {<<=,>>=};
to make two rules instead of four.
julia
next prev parent reply other threads:[~2017-06-17 6:00 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20170616174556.2358-1-fransklaver@gmail.com>
2017-06-16 22:44 ` endian bitshift defects [ was: staging: fusb302: don't bitshift __le16 type ] Joe Perches
2017-06-17 5:23 ` Julia Lawall
2017-06-17 5:50 ` Joe Perches
2017-06-17 6:00 ` Julia Lawall [this message]
2017-06-17 6:23 ` Joe Perches
2017-06-17 6:26 ` Julia Lawall
2017-06-23 22:29 ` Frans Klaver
2017-06-23 23:37 ` Julia Lawall
2017-06-26 8:06 ` Frans Klaver
2017-06-26 9:39 ` Julia Lawall
2017-06-26 20:57 ` Frans Klaver
2017-06-26 21:03 ` Julia Lawall
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.DEB.2.20.1706170753570.2045@hadrien \
--to=julia.lawall@lip6.fr \
--cc=devel@driverdev.osuosl.org \
--cc=fransklaver@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=gurooodas@gmail.com \
--cc=javier@dowhile0.org \
--cc=joe@perches.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=rmfrfs@gmail.com \
--cc=yueyao.zhu@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox