Re: add memset checks to checkpatch.pl

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Andy Whitcroft
Date: Thursday, September 2, 2010 - 3:52 am

On Wed, Aug 18, 2010 at 04:38:41PM -0700, Joe Perches wrote:

Generally we deal with these by simplifying the contained bracketed
sections.  This allows simpler textual comparisons on the result.
Something like the patch below.  Perhaps you could test the version at
the URL below and see if that does what you expected:

  http://www.kernel.org/pub/linux/kernel/people/apw/checkpatch/checkpatch.pl-testing

-apw

commit 46e399ce79f2e76578a3208e901eb50727c1e95e
Author: Andy Whitcroft <apw@canonical.com>
Date:   Thu Sep 2 11:47:41 2010 +0100

    checkpatch: check for common memset parameter issues
    
    Add checks for 0 and 1 used as lengths.  Generally these indicate badly
    ordered parameters.
    
    Based on patches by Dave Jones <davej@redhat.com> and
    Joe Perches <joe@perches.com>.
    
    Signed-off-by: Andy Whitcroft <apw@canonical.com>

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 5c60e16..6da84cc 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2719,6 +2719,26 @@ sub process {
 			WARN("sizeof(& should be avoided\n" . $herecurr);
 		}
 
+# Check for misused memsets
+		if (defined $stat && $stat =~ /\bmemset\s*\((.*)\)/s) {
+			my $args = $1;
+
+			# Flatten any parentheses and braces
+			while ($args =~ s/\([^\(\)]*\)/10/s ||
+			       $args =~ s/\{[^\{\}]*\}/10/s ||
+			       $args =~ s/\[[^\[\]]*\]/10/s)
+			{
+			}
+			# Extract the simplified arguments.
+			my ($ms_addr, $ms_val, $ms_size) =
+						split(/\s*,\s*/, $args);
+                       if ($ms_size =~ /^(0x|)0$/i) {
+                               ERROR("memset uses second argument as constant byte value, not third.\n" . $herecurr);
+                       } elsif ($ms_size =~ /^(0x|)1$/i) {
+                               WARN("single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . $herecurr);
+                       }
+		}
+
 # check for new externs in .c files.
 		if ($realfile =~ /\.c$/ && defined $stat &&
 		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
add memset checks to checkpatch.pl, Dave Jones, (Wed Aug 18, 1:40 pm)
Re: add memset checks to checkpatch.pl, Joe Perches, (Wed Aug 18, 2:32 pm)
Re: add memset checks to checkpatch.pl, Dave Jones, (Wed Aug 18, 2:48 pm)
Re: add memset checks to checkpatch.pl, Joe Perches, (Wed Aug 18, 2:57 pm)
Re: add memset checks to checkpatch.pl, Dave Jones, (Wed Aug 18, 3:17 pm)
Re: add memset checks to checkpatch.pl, Joe Perches, (Wed Aug 18, 4:38 pm)
Re: add memset checks to checkpatch.pl, Andy Whitcroft, (Thu Sep 2, 3:52 am)
Re: add memset checks to checkpatch.pl, Ævar Arnfjörð Bjarmason, (Thu Sep 2, 6:17 am)