Skip to content

Commit 0c1ef3b

Browse files
JoePerchesakpm00
authored andcommitted
checkpatch: add a couple new alloc functions to alloc with multiplies check
vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly. Add alloc-with-multiplies checks for these new functions. Simplify the original codes repeated else to use a hash. Link: KSPP#342 Link: https://lore.kernel.org/lkml/ZQCaO+tYycDxVLy7@work/ Link: https://lkml.kernel.org/r/edb667e19211652a32ef6069159bb85dbc3bcdfc.1694636817.git.joe@perches.com Original-patch-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Joe Perches <[email protected]> Cc: Andy Whitcroft <[email protected]> Cc: Dwaipayan Ray <[email protected]> Cc: Lukas Bulwahn <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 5179f95 commit 0c1ef3b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

scripts/checkpatch.pl

+14-7
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,16 @@ sub find_standard_signature {
834834
#Create a search pattern for all these strings to speed up a loop below
835835
our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')';
836836

837+
our %alloc_with_multiply_apis = (
838+
"kmalloc" => "kmalloc_array",
839+
"kvmalloc" => "kvmalloc_array",
840+
"vmalloc" => "vmalloc_array",
841+
"kvzalloc" => "kvcalloc",
842+
"kzalloc" => "kcalloc",
843+
"vzalloc" => "vcalloc",
844+
);
845+
our $alloc_with_multiply_search = '(?:' . join('|', keys %alloc_with_multiply_apis) . ')';
846+
837847
our $mode_perms_world_writable = qr{
838848
S_IWUGO |
839849
S_IWOTH |
@@ -7187,17 +7197,14 @@ sub process {
71877197
"Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
71887198
}
71897199

7190-
# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
7200+
# check for various allocs with multiplies that should use safer functions
71917201
if ($perl_version_ok &&
71927202
defined $stat &&
7193-
$stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
7203+
$stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*($alloc_with_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
71947204
my $oldfunc = $3;
7205+
my $newfunc = $alloc_with_multiply_apis{$oldfunc};
71957206
my $a1 = $4;
71967207
my $a2 = $10;
7197-
my $newfunc = "kmalloc_array";
7198-
$newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
7199-
$newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
7200-
$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
72017208
my $r1 = $a1;
72027209
my $r2 = $a2;
72037210
if ($a1 =~ /^sizeof\s*\S/) {
@@ -7213,7 +7220,7 @@ sub process {
72137220
"Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
72147221
$cnt == 1 &&
72157222
$fix) {
7216-
$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
7223+
$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
72177224
}
72187225
}
72197226
}

0 commit comments

Comments
 (0)