public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] scripts: checkpatch.pl: add 2 new checks on memset calls
@ 2015-02-25  2:40 Aya Mahfouz
  2015-02-25  3:10 ` Joe Perches
  0 siblings, 1 reply; 10+ messages in thread
From: Aya Mahfouz @ 2015-02-25  2:40 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches, linux-kernel

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.

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.

Cc: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Aya Mahfouz <mahfouz.saif.elyazal@gmail.com>
---
v2: adjusted all checks on memset calls to be in one body

 scripts/checkpatch.pl | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d124359..ab43d1b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4890,10 +4890,10 @@ 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) {
+		    $line =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
 
 			my $ms_addr = $2;
 			my $ms_val = $7;
@@ -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",
+					 "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)/;
+				}
 			}
 		}
 
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-02-27  1:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-25  2:40 [PATCH v2] scripts: checkpatch.pl: add 2 new checks on memset calls Aya Mahfouz
2015-02-25  3:10 ` Joe Perches
2015-02-25  4:35   ` Aya Mahfouz
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox