mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Add --strict test for kmalloc/kzalloc with multiply
@ 2014-05-13 23:48 Joe Perches
  2014-05-15 22:58 ` Andrew Morton
  0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2014-05-13 23:48 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Andy Whitcroft, LKML

Protect against sizeof overflows by preferring
kmalloc_array and kcalloc to kmalloc/kzalloc
with a sizeof multiply.

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index e7ff52a..33eb71f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4378,6 +4378,20 @@ sub process {
 			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
 		}
 
+# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
+		if ($^V && $^V ge 5.10.0 &&
+		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/) {
+			my $oldfunc = $3;
+			my $a1 = $4;
+			my $a2 = $10;
+			my $newfunc = "kmalloc_array";
+			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
+			if ($a1 =~ /^sizeof\s*\S/ || $a2 =~ /^sizeof\s*\S/) {
+				CHK("ALLOC_WITH_MULTIPLY",
+				    "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr);
+			}
+		}
+
 # check for krealloc arg reuse
 		if ($^V && $^V ge 5.10.0 &&
 		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {



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

end of thread, other threads:[~2014-05-15 23:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-13 23:48 [PATCH] checkpatch: Add --strict test for kmalloc/kzalloc with multiply Joe Perches
2014-05-15 22:58 ` Andrew Morton
2014-05-15 23:04   ` Joe Perches
2014-05-15 23:07     ` Andrew Morton
2014-05-15 23:27       ` [PATCH V2] checkpatch: Add warning " Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®