]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
checkpatch: add --strict preference for p = kmalloc(sizeof(*p)...
authorJoe Perches <joe@perches.com>
Wed, 3 Jul 2013 22:05:21 +0000 (15:05 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 3 Jul 2013 23:07:44 +0000 (16:07 -0700)
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>
scripts/checkpatch.pl

index 517da26d9a2d290bb798c4b4455311578beb5f53..6185eb67df94ba24300893634207f53a830a462c 100755 (executable)
@@ -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*,/) {