1 /// Remove casting the values returned by memory allocation functions
2 /// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
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.
11 // Copyright: (C) 2014 Himangi Saraogi GPLv2.
12 // Copyright: (C) 2017 Himanshu Jha GPLv2.
14 // Options: --no-includes --include-headers
26 m = re.compile(pattern)
28 @r1 depends on context || patch@
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\)(...)
40 //----------------------------------------------------------
42 //----------------------------------------------------------
44 @script:python depends on context@
48 if m.search(t) != None:
49 cocci.include_match(False)
51 @depends on context && r1@
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\)(...)
63 //----------------------------------------------------------
65 //----------------------------------------------------------
67 @script:python depends on patch@
71 if m.search(t) != None:
72 cocci.include_match(False)
74 @depends on patch && r1@
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\)(...)
86 //----------------------------------------------------------
87 // For org and report mode
88 //----------------------------------------------------------
90 @r2 depends on org || report@
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\)(...)
103 @script:python depends on org@
108 if m.search(t) != None:
109 cocci.include_match(False)
111 coccilib.org.print_safe_todo(p[0], t)
113 @script:python depends on report@
118 if m.search(t) != None:
119 cocci.include_match(False)
121 msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
122 coccilib.report.print_report(p[0], msg)