xref: /linux/scripts/coccinelle/api/alloc/alloc_cast.cocci (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
1/// Remove casting the values returned by memory allocation functions
2/// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
3///
4//# This makes an effort to find cases of casting of values returned by
5//# kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc,
6//# kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes
7//# the casting as it is not required. The result in the patch case may
8//# need some reformatting.
9//
10// Confidence: High
11// Copyright: (C) 2014 Himangi Saraogi GPLv2.
12// Copyright: (C) 2017 Himanshu Jha GPLv2.
13// Comments:
14// Options: --no-includes --include-headers
15//
16
17virtual context
18virtual patch
19virtual org
20virtual report
21
22@initialize:python@
23@@
24import re
25pattern = '__'
26m = re.compile(pattern)
27
28@r1 depends on context || patch@
29type T;
30@@
31
32  (T *)
33  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
34   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
35   dma_alloc_coherent\|dma_zalloc_coherent\|devm_kmalloc\|devm_kzalloc\|
36   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
37   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
38   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
39
40//----------------------------------------------------------
41//  For context mode
42//----------------------------------------------------------
43
44@script:python depends on context@
45t << r1.T;
46@@
47
48if m.search(t) != None:
49        cocci.include_match(False)
50
51@depends on context && r1@
52type r1.T;
53@@
54
55* (T *)
56  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
57   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
58   dma_alloc_coherent\|dma_zalloc_coherent\|devm_kmalloc\|devm_kzalloc\|
59   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
60   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
61   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
62
63//----------------------------------------------------------
64//  For patch mode
65//----------------------------------------------------------
66
67@script:python depends on patch@
68t << r1.T;
69@@
70
71if m.search(t) != None:
72        cocci.include_match(False)
73
74@depends on patch && r1@
75type r1.T;
76@@
77
78- (T *)
79  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
80   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
81   dma_alloc_coherent\|dma_zalloc_coherent\|devm_kmalloc\|devm_kzalloc\|
82   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
83   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
84   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
85
86//----------------------------------------------------------
87//  For org and report mode
88//----------------------------------------------------------
89
90@r2 depends on org || report@
91type T;
92position p;
93@@
94
95 (T@p *)
96  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
97   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
98   dma_alloc_coherent\|dma_zalloc_coherent\|devm_kmalloc\|devm_kzalloc\|
99   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
100   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
101   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
102
103@script:python depends on org@
104p << r2.p;
105t << r2.T;
106@@
107
108if m.search(t) != None:
109	cocci.include_match(False)
110else:
111	coccilib.org.print_safe_todo(p[0], t)
112
113@script:python depends on report@
114p << r2.p;
115t << r2.T;
116@@
117
118if m.search(t) != None:
119	cocci.include_match(False)
120else:
121	msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
122	coccilib.report.print_report(p[0], msg)
123