public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scripts: checkpatch.pl: add 2 new checks on memset calls
@ 2015-02-23 19:18 Aya Mahfouz
  2015-02-24  3:06 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Aya Mahfouz @ 2015-02-23 19:18 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>
---
 scripts/checkpatch.pl | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d124359..9619882 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4918,6 +4918,25 @@ sub process {
 			}
 		}
 
+# Check for memset(foo, 0x00|0xff, ETH_ALEN) that could be eth_zero_addr(foo)|eth_broadcast_addr(foo)
+		if ($^V && $^V ge 5.10.0 &&
+		    $line =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
+			my $num = $7;
+			if ($num =~ "0x00" && 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($2)/;
+			}
+			elsif ($num =~ "0xff" && 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($2)/;
+			}
+		}
+
+
 # typecasts on min/max could be min_t/max_t
 		if ($^V && $^V ge 5.10.0 &&
 		    defined $stat &&
-- 
1.9.3


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

* Re: [PATCH] scripts: checkpatch.pl: add 2 new checks on memset calls
  2015-02-23 19:18 [PATCH] scripts: checkpatch.pl: add 2 new checks on memset calls Aya Mahfouz
@ 2015-02-24  3:06 ` Joe Perches
  2015-02-25  2:53   ` Aya Mahfouz
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2015-02-24  3:06 UTC (permalink / raw)
  To: Aya Mahfouz; +Cc: Andy Whitcroft, linux-kernel

On Mon, 2015-02-23 at 21:18 +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.
> 
> 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.

Hello Aya.

Good idea but:

there already is a test for memset at (-next) line 4893.

Please extend that as below instead of adding another test.

> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -4918,6 +4918,25 @@ sub process {
>  			}
>  		}
>  
> +# Check for memset(foo, 0x00|0xff, ETH_ALEN) that could be eth_zero_addr(foo)|eth_broadcast_addr(foo)
> +		if ($^V && $^V ge 5.10.0 &&
> +		    $line =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
> +			my $num = $7;
> +			if ($num =~ "0x00" && WARN("PREFER_ETH_ZERO_ADDR",

# Check for misused memsets
		if ($^V && $^V ge 5.10.0 &&
		    defined $stat &&
		    $stat =~ /^\+(?:.*?)\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",
				 "..." . 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",
				      "..." . 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 (...)



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

* Re: [PATCH] scripts: checkpatch.pl: add 2 new checks on memset calls
  2015-02-24  3:06 ` Joe Perches
@ 2015-02-25  2:53   ` Aya Mahfouz
  0 siblings, 0 replies; 3+ messages in thread
From: Aya Mahfouz @ 2015-02-25  2:53 UTC (permalink / raw)
  To: Joe Perches; +Cc: Andy Whitcroft, linux-kernel

On Mon, Feb 23, 2015 at 07:06:27PM -0800, Joe Perches wrote:
> On Mon, 2015-02-23 at 21:18 +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.
> > 
> > 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.
> 
> Hello Aya.
> 
> Good idea but:
> 
> there already is a test for memset at (-next) line 4893.
> 
> Please extend that as below instead of adding another test.
> 

Hello Joe,

I've adjusted the checks as you requested. Kindly let me know if there
is something else that should be done.

Kind Regards,
Aya Saif El-yazal Mahfouz

> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -4918,6 +4918,25 @@ sub process {
> >  			}
> >  		}
> >  
> > +# Check for memset(foo, 0x00|0xff, ETH_ALEN) that could be eth_zero_addr(foo)|eth_broadcast_addr(foo)
> > +		if ($^V && $^V ge 5.10.0 &&
> > +		    $line =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
> > +			my $num = $7;
> > +			if ($num =~ "0x00" && WARN("PREFER_ETH_ZERO_ADDR",
> 
> # Check for misused memsets
> 		if ($^V && $^V ge 5.10.0 &&
> 		    defined $stat &&
> 		    $stat =~ /^\+(?:.*?)\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",
> 				 "..." . 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",
> 				      "..." . 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 (...)
> 
> 

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

end of thread, other threads:[~2015-02-25  2:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-23 19:18 [PATCH] scripts: checkpatch.pl: add 2 new checks on memset calls Aya Mahfouz
2015-02-24  3:06 ` Joe Perches
2015-02-25  2:53   ` Aya Mahfouz

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