diff options
author | Joe Perches <joe@perches.com> | 2013-07-03 22:05:21 (GMT) |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-07-03 23:07:44 (GMT) |
commit | a640d25cead66457ac14a878234f8b323ba8aade (patch) | |
tree | 68226ed2019c2e999efdbf6ccd8d0c53a271e833 | |
parent | 95e2c6023b0e4c8499fb521697f79215f69135fe (diff) | |
download | linux-a640d25cead66457ac14a878234f8b323ba8aade.tar.xz |
checkpatch: add --strict preference for p = kmalloc(sizeof(*p)...
Add another test for memory allocation style to follow
Documentation/CodingStyle:
Chapter 14: Allocating memory
The preferred form for passing a size of a struct is the following:
p = kmalloc(sizeof(*p), ...);
Signed-off-by: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rwxr-xr-x | scripts/checkpatch.pl | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 517da26..6185eb6 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -3507,6 +3507,14 @@ sub process { "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); } +# alloc style +# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) + if ($^V && $^V ge 5.10.0 && + $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { + CHK("ALLOC_SIZEOF_STRUCT", + "Prefer $3(sizeof(*$1)...) over $3($4...)\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*,/) { |