From: Aya Mahfouz <mahfouz.saif.elyazal@gmail.com>
To: Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH V4] checkpatch: prefer eth_<foo>_addr over memset(,,ETH_ALEN)
Date: Mon, 2 Mar 2015 02:47:41 +0200 [thread overview]
Message-ID: <20150302004740.GA20530@waves> (raw)
Add 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.
replace memset by eth_broadcast_addr if the second argument is
the broadcast address (0xff). eth_broadcast_addr is a wrapper
function for memset that sets the passed array the broadcast
address. The size of the address has to be ETH_ALEN.
In addition, the regex assigned to the variable $stat has been
restricted to handle memset function calls of the form:
[<identifer> =]* memset(<arg1>, <arg2>, <arg3>)
Cc: Julia Lawall <julia.lawall@lip6.fr>
Cc: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Joe Perches <joe@perches.com>
Signed-off-by: Aya Mahfouz <mahfouz.saif.elyazal@gmail.com>
[joe@perches.com: handled all regular expressions]
Portions-by: Joe Perches<joe@perches.com>
---
v2: adjusted all checks on memset calls to be in one body
v3: adjusted all regular expressions per Joe Perches suggestions. Added
a Signed-off-by tag for Joe since all the important regular expressions
are written by him.
v4: changed the patch's subject and modified the tags section.
scripts/checkpatch.pl | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d124359..cbe973e 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4890,10 +4890,11 @@ sub process {
}
}
-# Check for misused memsets
+# Check for misused memsets then check for memset(foo, 0x00|0xff, ETH_ALEN)
+# calls that could be eth_zero_addr(foo)|eth_broadcast_addr(foo)
if ($^V && $^V ge 5.10.0 &&
defined $stat &&
- $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
+ $stat =~ /^\+(?:\s*$Ident\s*=)?\s*memset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
my $ms_addr = $2;
my $ms_val = $7;
@@ -4905,6 +4906,18 @@ sub process {
} elsif ($ms_size =~ /^(0x|)1$/i) {
WARN("MEMSET",
"single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
+ } elsif ($ms_val =~ /^(?:0x)?0+$/i &&
+ $ms_size =~ /^ETH_ALEN$/ &&
+ WARN("PREFER_ETH_ADDR",
+ "Prefer eth_zero_addr() over memset() if the second address is 0\n" . $herecurr) &&
+ $fix) {
+ $fixed[$fixlinenr] =~ s/\bmemset\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",
+ "Prefer eth_broadcast_addr() over memset() if the second address is 0xff\n" . $herecurr) &&
+ $fix) {
+ $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*\Q$ms_addr\E\s*,\s*\Q$ms_val\E\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($ms_addr)/;
}
}
--
1.9.3
--
Kind Regards,
Aya Saif El-yazal Mahfouz
reply other threads:[~2015-03-02 0:47 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20150302004740.GA20530@waves \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.