Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752552AbdFQFuI (ORCPT ); Sat, 17 Jun 2017 01:50:08 -0400 Received: from smtprelay0233.hostedemail.com ([216.40.44.233]:55770 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750841AbdFQFuG (ORCPT ); Sat, 17 Jun 2017 01:50:06 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::::::::,RULES_HIT:41:355:379:541:599:960:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2194:2199:2393:2553:2559:2562:2828:3138:3139:3140:3141:3142:3354:3622:3865:3866:3867:3868:3870:3871:3872:4250:4321:5007:9108:10004:10400:10848:11232:11658:11783:11914:12296:12679:12740:12895:13069:13095:13138:13231:13255:13311:13357:13439:13894:14040:14659:14721:21080:21324:21433:21434:21627:30012:30054:30079:30090:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: boot89_57d73cc03431b X-Filterd-Recvd-Size: 3063 Message-ID: <1497678601.10546.32.camel@perches.com> Subject: Re: endian bitshift defects [ was: staging: fusb302: don't bitshift __le16 type ] From: Joe Perches To: Julia Lawall Cc: Frans Klaver , Greg Kroah-Hartman , kernel-janitors , Guenter Roeck , Yueyao Zhu , Rui Miguel Silva , Guru Das Srinagesh , Javier Martinez Canillas , devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Date: Fri, 16 Jun 2017 22:50:01 -0700 In-Reply-To: References: <20170616174556.2358-1-fransklaver@gmail.com> <1497653077.10546.23.camel@perches.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.22.6-1ubuntu1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1792 Lines: 60 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... ] > > $ 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. > Would it be useful to add this to the scripts > in the kernel? Maybe. 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