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