checkpatch: Add --strict test for kmalloc/kzalloc with multiply
Joe Perches
maintainerPATCH 1/1 checkpatch: Add --strict test for kmalloc/kzalloc with multiply
Protect against sizeof overflows by preferringkmalloc_array and kcalloc to kmalloc/kzalloc
with a sizeof multiply.
--- scripts/checkpatch.pl | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
scripts/checkpatch.pl +14 -0
@@ -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*,/) {
Quoted 16 lines · On Tue, 13 May 2014 16:48:49 -0700 Joe Perches <[email protected]> wrote:
Protect against sizeof overflows by preferring
kmalloc_arrayand kcalloc to kmalloc/kzalloc
with a sizeof multiply....
scripts/checkpatch.pl +4
-0@@ -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;
scripts/checkpatch.pl sub process { · line 4394
Quoted 10 lines
scripts/checkpatch.pl +10
-0+ 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); + } + } +
Why hide this behind --strict?
Joe Perches
maintainerQuoted 4 lines · Andrew Morton
Quoted 3 lines · On Tue, 13 May 2014 16:48:49 -0700 Joe Perches <[email protected]> wrote:
Protect against sizeof overflows by preferring
kmalloc_arrayand kcalloc to kmalloc/kzalloc
with a sizeof multiply.
[]
Quoted 8 lines
Quoted 8 lines
scripts/checkpatch.pl +8
-0+# 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");
Quoted 8 lines
scripts/checkpatch.pl sub process { · line 4394
Quoted 6 lines
scripts/checkpatch.pl +6
-0+ if ($a1 =~ /^sizeof\s*\S/ || $a2 =~ /^sizeof\s*\S/) { + CHK("ALLOC_WITH_MULTIPLY", + "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr); + } + } +Why hide this behind --strict?
Non-obvious CHK/--strict tests are less controversial.
The block above it
"prefer foo = alloc(sizeof(*foo)) over foo = alloc(sizeof(struct bar))" used CHK so I copied it.
I've no objection to making it WARN instead,
Quoted 23 lines · On Thu, 15 May 2014 16:04:46 -0700 Joe Perches <[email protected]> wrote:
Quoted 4 lines · Andrew Morton
Quoted 3 lines · On Tue, 13 May 2014 16:48:49 -0700 Joe Perches <[email protected]> wrote:
Protect against sizeof overflows by preferring
kmalloc_arrayand kcalloc to kmalloc/kzalloc
with a sizeof multiply.[]
Quoted 8 lines
Quoted 8 lines
scripts/checkpatch.pl +8
-0+# 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");Quoted 8 lines
scripts/checkpatch.pl sub process { · line 4394
Quoted 6 lines
scripts/checkpatch.pl +6
-0+ if ($a1 =~ /^sizeof\s*\S/ || $a2 =~ /^sizeof\s*\S/) { + CHK("ALLOC_WITH_MULTIPLY", + "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr); + } + } +Why hide this behind --strict?
Quoted 7 lines
Non-obvious CHK/--strict tests are less controversial.
The block above it
"prefer foo = alloc(sizeof(*foo)) over foo = alloc(sizeof(struct bar))" used CHK so I copied it.I've no objection to making it WARN instead,
I'd prefer that - this is one of my regular comment-on-during-review things.
Joe Perches
maintainerPATCH v2 1/1 checkpatch: Add warning for kmalloc/kzalloc with multiply
Protect against sizeof overflows by preferring
kmalloc_array/kcalloc over kmalloc/kzalloc
with a sizeof multiply.
---
V2: Make it a WARN instead.
Add capability to --fix it too.
scripts/checkpatch.pl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
scripts/checkpatch.pl +24 -0
@@ -4378,6 +4378,30 @@ 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/) { + if (WARN("ALLOC_WITH_MULTIPLY", + "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) && + $fix) { + my $r1 = $a1; + my $r2 = $a2; + if ($a1 =~ /^sizeof\s*\S/) { + $r1 = $a2; + $r2 = $a1; + } + $fixed[$linenr - 1] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; + + } + } + } + # 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*,/) {