linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Test for kmalloc/memset(0) pairs
@ 2011-03-18  2:52 Steven Rostedt
  2011-03-18  3:15 ` Jonathan Corbet
  0 siblings, 1 reply; 28+ messages in thread
From: Steven Rostedt @ 2011-03-18  2:52 UTC (permalink / raw)
  To: LKML; +Cc: Andy Whitcroft, Dave Jones, Andrew Morton

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



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

end of thread, other threads:[~2011-03-24 20:41 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-18  2:52 [PATCH] checkpatch: Test for kmalloc/memset(0) pairs Steven Rostedt
2011-03-18  3:15 ` Jonathan Corbet
2011-03-18  3:32   ` Steven Rostedt
2011-03-20  3:40     ` Américo Wang
2011-03-20  7:02       ` Pekka Enberg
2011-03-20  8:01         ` Julia Lawall
2011-03-20  9:02           ` Pekka Enberg
2011-03-20  9:17             ` Julia Lawall
2011-03-20 10:54               ` Ingo Molnar
2011-03-20 11:06                 ` Pekka Enberg
2011-03-20 11:32                   ` Nicolas Palix
2011-03-20 12:00                     ` Julia Lawall
2011-03-20 11:09                 ` Alexey Dobriyan
2011-03-20 12:38                 ` Julia Lawall
2011-03-20 14:48                 ` Alejandro Riveira Fernández
2011-03-21 13:13                   ` Steven Rostedt
2011-03-24 15:34               ` Aneesh Kumar K. V
2011-03-24 16:08                 ` Julia Lawall
2011-03-24 16:10                   ` Nicolas Palix
2011-03-24 18:30                     ` Arnaud Lacombe
2011-03-24 20:39                       ` Julia Lawall
2011-03-24 17:28                   ` Aneesh Kumar K. V
2011-03-19 19:39   ` Dave Jones
2011-03-21 13:26     ` Steven Rostedt
2011-03-21 13:29       ` Steven Rostedt
2011-03-21 13:55       ` Borislav Petkov
2011-03-22 19:58       ` checkpatch: introduce --nocs to disable CodingStyle warnings Dave Jones
2011-03-22 20:21         ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).