[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <1300416744.16880.904.camel@gandalf.stny.rr.com>
Date: Thu, 17 Mar 2011 22:52:24 -0400
From: Steven Rostedt <rostedt@...dmis.org>
To: LKML <linux-kernel@...r.kernel.org>
Cc: Andy Whitcroft <apw@...dowen.org>, Dave Jones <davej@...hat.com>,
Andrew Morton <akpm@...ux-foundation.org>
Subject: [PATCH] checkpatch: Test for kmalloc/memset(0) pairs
The use of kzalloc() is preferred over kmalloc/memset(0) pairs.
When a match is made with "memset(p, 0, s);" a search back through the
patch hunk is made looking for "p = kmalloc(s,". If that is found, then
a warning is given, suggesting to use kzalloc() instead.
Signed-off-by: Steven Rostedt <rostedt@...dmis.org>
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 58848e3..f28f0e3 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2902,6 +2902,22 @@ sub process {
$line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
}
+
+# The use of kzalloc() is preferred over kmalloc/memset(0) pairs.
+ if ($line =~ /\smemset\s*\((\S*)\s*,\s*(?:0x0|0)\s*,\s*(\S*)\s*\);/) {
+ my $ptr = $1;
+ my $size = $2;
+
+ for (my $i = $linenr-2; $i >= 0; $i--) {
+ next if ($lines[$i] =~ /^-/); # ignore deletions
+ last if ($lines[$i] =~ /^\@\@/);
+ if ($lines[$i] =~ /\s(\S*)\s*=\s*kmalloc\((\S*)\,/ &&
+ $1 eq $ptr && $2 eq $size) {
+ WARN("use kzalloc() instead of kmalloc/memset(p,0,size) pair\n"
+ . $herecurr);
+ }
+ }
+ }
}
# If we have no input at all, then there is nothing to report on
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists