From: Aya Mahfouz <mahfouz.saif.elyazal@gmail.com>
To: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] scripts: checkpatch.pl: add 2 new checks on memset calls
Date: Wed, 25 Feb 2015 06:35:21 +0200 [thread overview]
Message-ID: <20150225043521.GB9220@localhost.localdomain> (raw)
In-Reply-To: <1424833852.11070.1.camel@perches.com>
On Tue, Feb 24, 2015 at 07:10:52PM -0800, Joe Perches wrote:
> On Wed, 2015-02-25 at 04:40 +0200, Aya Mahfouz wrote:
> > This patch adds 2 new checks on memset calls in the file
> > checkpatch.pl as follows:
> >
> > replace memset by eth_zero_addr if the second argument is
> > an address of zeros (0x00). eth_zero_addr is a wrapper function
> > for memset that takes an address array to set as zero. The size
> > address has to be ETH_ALEN.
>
> []
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -4901,10 +4901,22 @@ sub process {
> >
> > if ($ms_size =~ /^(0x|)0$/i) {
> > ERROR("MEMSET",
> > - "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
> > + "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$line\n");
> > } elsif ($ms_size =~ /^(0x|)1$/i) {
> > WARN("MEMSET",
> > - "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
> > + "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$line\n");
> > + } elsif ($ms_size =~ /^ETH_ALEN/i) {
> > + if ($ms_val =~ /^0x00/i && WARN("PREFER_ETH_ZERO_ADDR",
>
> This isn't right. Look again at what I suggested.
>
> This would match 0x00ff and wouldn't match 0
>
> > + "Prefer eth_zero_addr() over memset() if the second address is 0x00\n" . $herecurr) &&
> > + $fix) {
> > +
> > + $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/eth_zero_addr($ms_addr)/;
> > + } elsif ($ms_val =~ /^0xff/i && WARN("PREFER_ETH_BROADCAST_ADDR",
> > + "Prefer eth_broadcast_addr() over memset() if the second address is 0xff\n" . $herecurr) &&
> > + $fix) {
> > +
> > + $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/eth_broadcast_addr($ms_addr)/;
> > + }
> > }
> > }
> >
>
>
>
ok, I didn't see your suggestion, sorry. Can you look at the following
modification before sending the third patch? I don't use $stat because
I get false positives with it.
#check for misused memsets
if ($^V && $^V ge 5.10.0 &&
$line =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
my $ms_addr = $2;
my $ms_val = $7;
my $ms_size = $12;
if ($ms_val =~ /^(?:0|0x0+)$/i &&
$ms_size =~ /^ETH_ALEN$/ &&
WARN("PREFER_ETH_ADDR_FUNC",
"Prefer eth_zero_addr() over memset() if the second address is 0x00\n" . $herecurr) &&
$fix) {
$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*\Q$ms_addr\E\s*,\s*\Q$ms_val\E\s*\,\s*ETH_ALEN\s*\)/eth_zero_addr($ms_addr)/;
} elsif ($ms_val =~ /^(?:0xff|255)$/i &&
$ms_size =~ /^ETH_ALEN$/ &&
WARN("PREFER_ETH_ADDR_FUNC",
"Prefer eth_broadcast_addr() over memset() if the second address is 0xff\n" . $herecurr) &&
$fix) {
$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*\Q$ms_addr\E\s*,\s*\Q$ms_val\E\s*\,\s*ETH_ALEN\s*\)/eth_broadcast_addr($ms_addr)/;
} elsif ($ms_size =~ /^(0x|)0$/i) {
ERROR("MEMSET",
"memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$line\n");
} elsif ($ms_size =~ /^(0x|)1$/i) {
WARN("MEMSET",
"single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$line\n");
}
}
next prev parent reply other threads:[~2015-02-25 4:35 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-25 2:40 Aya Mahfouz
2015-02-25 3:10 ` Joe Perches
2015-02-25 4:35 ` Aya Mahfouz [this message]
2015-02-25 4:41 ` Joe Perches
2015-02-25 4:59 ` Aya Mahfouz
2015-02-25 5:45 ` Joe Perches
2015-02-25 6:08 ` Aya Mahfouz
2015-02-25 6:20 ` Joe Perches
2015-02-27 1:42 ` Aya Mahfouz
2015-02-27 1:57 ` Joe Perches
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=20150225043521.GB9220@localhost.localdomain \
--to=mahfouz.saif.elyazal@gmail.com \
--cc=apw@canonical.com \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
/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
all inboxes | Powered by JetHome®