Skip to content

Commit a640d25

Browse files
JoePerchestorvalds
authored andcommitted
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>
1 parent 95e2c60 commit a640d25

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,6 +3507,14 @@ sub process {
35073507
"unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
35083508
}
35093509

3510+
# alloc style
3511+
# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3512+
if ($^V && $^V ge 5.10.0 &&
3513+
$line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3514+
CHK("ALLOC_SIZEOF_STRUCT",
3515+
"Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3516+
}
3517+
35103518
# check for krealloc arg reuse
35113519
if ($^V && $^V ge 5.10.0 &&
35123520
$line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {

0 commit comments

Comments
 (0)