18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/// Remove casting the values returned by memory allocation functions
38c2ecf20Sopenharmony_ci/// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
48c2ecf20Sopenharmony_ci///
58c2ecf20Sopenharmony_ci//# This makes an effort to find cases of casting of values returned by
68c2ecf20Sopenharmony_ci//# kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc,
78c2ecf20Sopenharmony_ci//# kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes
88c2ecf20Sopenharmony_ci//# the casting as it is not required. The result in the patch case may
98c2ecf20Sopenharmony_ci//# need some reformatting.
108c2ecf20Sopenharmony_ci//
118c2ecf20Sopenharmony_ci// Confidence: High
128c2ecf20Sopenharmony_ci// Copyright: (C) 2014 Himangi Saraogi
138c2ecf20Sopenharmony_ci// Copyright: (C) 2017 Himanshu Jha
148c2ecf20Sopenharmony_ci// Comments:
158c2ecf20Sopenharmony_ci// Options: --no-includes --include-headers
168c2ecf20Sopenharmony_ci//
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_civirtual context
198c2ecf20Sopenharmony_civirtual patch
208c2ecf20Sopenharmony_civirtual org
218c2ecf20Sopenharmony_civirtual report
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci@initialize:python@
248c2ecf20Sopenharmony_ci@@
258c2ecf20Sopenharmony_ciimport re
268c2ecf20Sopenharmony_cipattern = '__'
278c2ecf20Sopenharmony_cim = re.compile(pattern)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci@r1 depends on context || patch@
308c2ecf20Sopenharmony_citype T;
318c2ecf20Sopenharmony_ci@@
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci  (T *)
348c2ecf20Sopenharmony_ci  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
358c2ecf20Sopenharmony_ci   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
368c2ecf20Sopenharmony_ci   dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
378c2ecf20Sopenharmony_ci   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
388c2ecf20Sopenharmony_ci   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
398c2ecf20Sopenharmony_ci   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci//----------------------------------------------------------
428c2ecf20Sopenharmony_ci//  For context mode
438c2ecf20Sopenharmony_ci//----------------------------------------------------------
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci@script:python depends on context@
468c2ecf20Sopenharmony_cit << r1.T;
478c2ecf20Sopenharmony_ci@@
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciif m.search(t) != None:
508c2ecf20Sopenharmony_ci        cocci.include_match(False)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci@depends on context && r1@
538c2ecf20Sopenharmony_citype r1.T;
548c2ecf20Sopenharmony_ci@@
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci* (T *)
578c2ecf20Sopenharmony_ci  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
588c2ecf20Sopenharmony_ci   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
598c2ecf20Sopenharmony_ci   dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
608c2ecf20Sopenharmony_ci   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
618c2ecf20Sopenharmony_ci   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
628c2ecf20Sopenharmony_ci   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci//----------------------------------------------------------
658c2ecf20Sopenharmony_ci//  For patch mode
668c2ecf20Sopenharmony_ci//----------------------------------------------------------
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci@script:python depends on patch@
698c2ecf20Sopenharmony_cit << r1.T;
708c2ecf20Sopenharmony_ci@@
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciif m.search(t) != None:
738c2ecf20Sopenharmony_ci        cocci.include_match(False)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci@depends on patch && r1@
768c2ecf20Sopenharmony_citype r1.T;
778c2ecf20Sopenharmony_ci@@
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci- (T *)
808c2ecf20Sopenharmony_ci  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
818c2ecf20Sopenharmony_ci   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
828c2ecf20Sopenharmony_ci   dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
838c2ecf20Sopenharmony_ci   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
848c2ecf20Sopenharmony_ci   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
858c2ecf20Sopenharmony_ci   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci//----------------------------------------------------------
888c2ecf20Sopenharmony_ci//  For org and report mode
898c2ecf20Sopenharmony_ci//----------------------------------------------------------
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci@r2 depends on org || report@
928c2ecf20Sopenharmony_citype T;
938c2ecf20Sopenharmony_ciposition p;
948c2ecf20Sopenharmony_ci@@
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci (T@p *)
978c2ecf20Sopenharmony_ci  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
988c2ecf20Sopenharmony_ci   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
998c2ecf20Sopenharmony_ci   dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
1008c2ecf20Sopenharmony_ci   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
1018c2ecf20Sopenharmony_ci   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
1028c2ecf20Sopenharmony_ci   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci@script:python depends on org@
1058c2ecf20Sopenharmony_cip << r2.p;
1068c2ecf20Sopenharmony_cit << r2.T;
1078c2ecf20Sopenharmony_ci@@
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciif m.search(t) != None:
1108c2ecf20Sopenharmony_ci	cocci.include_match(False)
1118c2ecf20Sopenharmony_cielse:
1128c2ecf20Sopenharmony_ci	coccilib.org.print_safe_todo(p[0], t)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci@script:python depends on report@
1158c2ecf20Sopenharmony_cip << r2.p;
1168c2ecf20Sopenharmony_cit << r2.T;
1178c2ecf20Sopenharmony_ci@@
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciif m.search(t) != None:
1208c2ecf20Sopenharmony_ci	cocci.include_match(False)
1218c2ecf20Sopenharmony_cielse:
1228c2ecf20Sopenharmony_ci	msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
1238c2ecf20Sopenharmony_ci	coccilib.report.print_report(p[0], msg)
124