[PATCH] checkpatch: Test for kmalloc/memset(0) pairs

From: Steven Rostedt
Date: Thu Mar 17 2011 - 22:52:33 EST


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@xxxxxxxxxxx>

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@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/