checkpatch: refactor and add new alloc w/ multiply tests
Joe Perches
maintainercover checkpatch: refactor and add new alloc w/ multiply tests
Original alloc function patch by Gustavo Silva
Joe Perches (2):
checkpatch: Simplify creating search strings
checkpatch: Add a couple new alloc functions to alloc with multiplies check
scripts/checkpatch.pl | 55 +++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 34 deletions(-)
Signature · 1 line
2.41.0
Joe Perches
maintainerPATCH 1/2 checkpatch: Simplify creating search strings
Use join and map instead of loops.
--- scripts/checkpatch.pl | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)
scripts/checkpatch.pl +7 -27
@@ -625,18 +625,8 @@ our $signature_tags = qr{(?xi: our @link_tags = qw(Link Closes); #Create a search and print patterns for all these strings to be used directly below -our $link_tags_search = ""; -our $link_tags_print = ""; -foreach my $entry (@link_tags) { - if ($link_tags_search ne "") { - $link_tags_search .= '|'; - $link_tags_print .= ' or '; - } - $entry .= ':'; - $link_tags_search .= $entry; - $link_tags_print .= "'$entry'"; -} -$link_tags_search = "(?:${link_tags_search})"; +our $link_tags_search = '(?:' . join('|', @link_tags) . ')'; +our $link_tags_print = "'" . join("' or '", @link_tags) . "'"; our $tracing_logging_tags = qr{(?xi: [=-]*> |
@@ -819,15 +809,10 @@ our @mode_permission_funcs = ( ["__ATTR", 2], ); -my $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; - #Create a search pattern for all these functions to speed up a loop below -our $mode_perms_search = ""; -foreach my $entry (@mode_permission_funcs) { - $mode_perms_search .= '|' if ($mode_perms_search ne ""); - $mode_perms_search .= $entry->[0]; -} -$mode_perms_search = "(?:${mode_perms_search})"; +our $mode_perms_search = '(?:' . join('|', map{$_->[0]} @mode_permission_funcs) . ')'; + +my $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; our %deprecated_apis = ( "synchronize_rcu_bh" => "synchronize_rcu",
@@ -847,12 +832,7 @@ our %deprecated_apis = ( ); #Create a search pattern for all these strings to speed up a loop below -our $deprecated_apis_search = ""; -foreach my $entry (keys %deprecated_apis) { - $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); - $deprecated_apis_search .= $entry; -} -$deprecated_apis_search = "(?:${deprecated_apis_search})"; +our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')'; our $mode_perms_world_writable = qr{ S_IWUGO |
@@ -887,7 +867,7 @@ foreach my $entry (keys %mode_permission_string_types) { $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); $mode_perms_string_search .= $entry; } -our $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; +our $single_mode_perms_string_search = '(?:' . join('|', keys %mode_permission_string_types) . ')'; our $multi_mode_perms_string_search = qr{ ${single_mode_perms_string_search} (?:\s*\|\s*${single_mode_perms_string_search})*
Signature · 1 line
2.41.0
Joe Perches
maintainerPATCH 2/2 checkpatch: Add a couple new alloc functions to alloc with multiplies check
In reply to Joe Perchesvmalloc() 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.
--- scripts/checkpatch.pl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
scripts/checkpatch.pl +14 -7
@@ -834,6 +834,16 @@ our %deprecated_apis = ( #Create a search pattern for all these strings to speed up a loop below our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')'; +our %alloc_with_multiply_apis = ( + "kmalloc" => "kmalloc_array", + "kvmalloc" => "kvmalloc_array", + "vmalloc" => "vmalloc_array", + "kvzalloc" => "kvcalloc", + "kzalloc" => "kcalloc", + "vzalloc" => "vcalloc", +); +our $alloc_with_multiply_search = '(?:' . join('|', keys %alloc_with_multiply_apis) . ')'; + our $mode_perms_world_writable = qr{ S_IWUGO | S_IWOTH |
@@ -7187,17 +7197,14 @@ sub process { "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); } -# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc +# check for various allocs with multiplies that should use safer functions if ($perl_version_ok && defined $stat && - $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { + $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*($alloc_with_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { my $oldfunc = $3; + my $newfunc = $alloc_with_multiply_apis{$oldfunc}; my $a1 = $4; my $a2 = $10; - my $newfunc = "kmalloc_array"; - $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc"); - $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc"); - $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); my $r1 = $a1; my $r2 = $a2; if ($a1 =~ /^sizeof\s*\S/) {
@@ -7213,7 +7220,7 @@ sub process { "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && $cnt == 1 && $fix) { - $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; + $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; } } }
Signature · 1 line
2.41.0
Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check
Quoted 10 lines · Joe Perches
vmalloc()andvzalloc()functions have now 2-factor multiplication argument formsvmalloc_array()andvcalloc(), correspondingly.Add alloc-with-multiplies checks for these new functions.
Simplify the original codes repeated else to use a hash.
Original-patch-by Gustavo A. R. Silva <[email protected]>
Why don't you wait for a response or a v2 from the original submitter?
--
Gustavo
Joe Perches
maintainerRe: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check
Quoted 7 lines · Gustavo A. R. Silva
Quoted 5 lines · Joe Perches
vmalloc()andvzalloc()functions have now 2-factor multiplication argument formsvmalloc_array()andvcalloc(), correspondingly.Add alloc-with-multiplies checks for these new functions.
Quoted 8 lines
Quoted 5 lines
Simplify the original codes repeated else to use a hash.
Original-patch-by Gustavo A. R. Silva <[email protected]>Why don't you wait for a response or a v2 from the original submitter?
Because there really is no need to wait.
Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check
Quoted 10 lines · Joe Perches
Quoted 9 lines · Gustavo A. R. Silva
Quoted 7 lines · Joe Perches
vmalloc()andvzalloc()functions have now 2-factor multiplication argument formsvmalloc_array()andvcalloc(), correspondingly.Add alloc-with-multiplies checks for these new functions.
Simplify the original codes repeated else to use a hash.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Original-patch-by Gustavo A. R. Silva <[email protected]>Why don't you wait for a response or a v2 from the original submitter?
Because there really is no need to wait.
By the way, did you test it?
--
Gustavo
Joe Perches
maintainerRe: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check
Quoted 14 lines · Gustavo A. R. Silva
Quoted 12 lines · Joe Perches
Quoted 11 lines · Gustavo A. R. Silva
Quoted 9 lines · Joe Perches
vmalloc()andvzalloc()functions have now 2-factor multiplication argument formsvmalloc_array()andvcalloc(), correspondingly.Add alloc-with-multiplies checks for these new functions.
Simplify the original codes repeated else to use a hash.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Original-patch-by Gustavo A. R. Silva <[email protected]>Why don't you wait for a response or a v2 from the original submitter?
Because there really is no need to wait.
By the way, did you test it?
Yes, against arch/s390/include/asm/idals.h
Did you? If so, you could also add your own tested-by...
Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check
Quoted 17 lines · Joe Perches
Quoted 16 lines · Gustavo A. R. Silva
Quoted 14 lines · Joe Perches
Quoted 13 lines · Gustavo A. R. Silva
Quoted 10 lines · Joe Perches
vmalloc()andvzalloc()functions have now 2-factor multiplication argument formsvmalloc_array()andvcalloc(), correspondingly.Add alloc-with-multiplies checks for these new functions.
Simplify the original codes repeated else to use a hash.
Original-patch-by Gustavo A. R. Silva <[email protected]>
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Why don't you wait for a response or a v2 from the original submitter?
Because there really is no need to wait.
By the way, did you test it?
Yes, against
arch/s390/include/asm/idals.h
I don't see any instances of vmalloc() or vzalloc() in that file.
--
Gustavo
Joe Perches
maintainerRe: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check
Quoted 22 lines · Gustavo A. R. Silva
Quoted 20 lines · Joe Perches
Quoted 19 lines · Gustavo A. R. Silva
Quoted 17 lines · Joe Perches
Quoted 15 lines · Gustavo A. R. Silva
Quoted 10 lines · Joe Perches
vmalloc()andvzalloc()functions have now 2-factor multiplication argument formsvmalloc_array()andvcalloc(), correspondingly.Add alloc-with-multiplies checks for these new functions.
Simplify the original codes repeated else to use a hash.
Original-patch-by Gustavo A. R. Silva <[email protected]>Why don't you wait for a response or a v2 from the original submitter?
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Because there really is no need to wait.
By the way, did you test it?
Yes, against
arch/s390/include/asm/idals.hI don't see any instances of
vmalloc()orvzalloc()in that file.
<snort> Cute.
Missing a ? in the original search '\s*,?'
or just its removal...
Likely the removal would be better.
thanks, cheers, Joe
Joe Perches
maintainerPATCH v2 2/2 checkpatch: Add a couple new alloc functions to alloc with multiplies check
In reply to Joe Perchesvmalloc() 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.
---
v2: Fix search because vmalloc functions don't take a 3rd argument
scripts/checkpatch.pl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
scripts/checkpatch.pl +14 -7
@@ -834,6 +834,16 @@ our %deprecated_apis = ( #Create a search pattern for all these strings to speed up a loop below our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')'; +our %alloc_with_multiply_apis = ( + "kmalloc" => "kmalloc_array", + "kvmalloc" => "kvmalloc_array", + "vmalloc" => "vmalloc_array", + "kvzalloc" => "kvcalloc", + "kzalloc" => "kcalloc", + "vzalloc" => "vcalloc", +); +our $alloc_with_multiply_search = '(?:' . join('|', keys %alloc_with_multiply_apis) . ')'; + our $mode_perms_world_writable = qr{ S_IWUGO | S_IWOTH |
@@ -7187,17 +7197,14 @@ sub process { "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); } -# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc +# check for various allocs with multiplies that should use safer functions if ($perl_version_ok && defined $stat && - $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { + $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*($alloc_with_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/) { my $oldfunc = $3; + my $newfunc = $alloc_with_multiply_apis{$oldfunc}; my $a1 = $4; my $a2 = $10; - my $newfunc = "kmalloc_array"; - $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc"); - $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc"); - $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); my $r1 = $a1; my $r2 = $a2; if ($a1 =~ /^sizeof\s*\S/) {
@@ -7213,7 +7220,7 @@ sub process { "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && $cnt == 1 && $fix) { - $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; + $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; } } }
Signature · 1 line
2.41.0