~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/coccinelle/api/alloc/alloc_cast.cocci

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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)

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php