lkml.org 
[lkml]   [2011]   [Mar]   [17]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Subject[PATCH] checkpatch: Test for kmalloc/memset(0) pairs
From
Date
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@goodmis.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



\
 
 \ /
  Last update: 2011-03-18 03:55    [W:0.481 / U:0.136 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site