alloc_cast.cocci 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. virtual context
  17. virtual patch
  18. virtual org
  19. virtual report
  20. @initialize:python@
  21. @@
  22. import re
  23. pattern = '__'
  24. m = re.compile(pattern)
  25. @r1 depends on context || patch@
  26. type T;
  27. @@
  28. (T *)
  29. \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
  30. kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
  31. dma_alloc_coherent\|dma_zalloc_coherent\|devm_kmalloc\|devm_kzalloc\|
  32. kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
  33. pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
  34. kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
  35. //----------------------------------------------------------
  36. // For context mode
  37. //----------------------------------------------------------
  38. @script:python depends on context@
  39. t << r1.T;
  40. @@
  41. if m.search(t) != None:
  42. cocci.include_match(False)
  43. @depends on context && r1@
  44. type r1.T;
  45. @@
  46. * (T *)
  47. \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
  48. kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
  49. dma_alloc_coherent\|dma_zalloc_coherent\|devm_kmalloc\|devm_kzalloc\|
  50. kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
  51. pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
  52. kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
  53. //----------------------------------------------------------
  54. // For patch mode
  55. //----------------------------------------------------------
  56. @script:python depends on patch@
  57. t << r1.T;
  58. @@
  59. if m.search(t) != None:
  60. cocci.include_match(False)
  61. @depends on patch && r1@
  62. type r1.T;
  63. @@
  64. - (T *)
  65. \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
  66. kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
  67. dma_alloc_coherent\|dma_zalloc_coherent\|devm_kmalloc\|devm_kzalloc\|
  68. kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
  69. pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
  70. kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
  71. //----------------------------------------------------------
  72. // For org and report mode
  73. //----------------------------------------------------------
  74. @r2 depends on org || report@
  75. type T;
  76. position p;
  77. @@
  78. (T@p *)
  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. @script:python depends on org@
  86. p << r2.p;
  87. t << r2.T;
  88. @@
  89. if m.search(t) != None:
  90. cocci.include_match(False)
  91. else:
  92. coccilib.org.print_safe_todo(p[0], t)
  93. @script:python depends on report@
  94. p << r2.p;
  95. t << r2.T;
  96. @@
  97. if m.search(t) != None:
  98. cocci.include_match(False)
  99. else:
  100. msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
  101. coccilib.report.print_report(p[0], msg)