alloc_cast.cocci 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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: 2014, Himangi Saraogi GPLv2.
  12. // Comments:
  13. // Options: --no-includes --include-headers
  14. //
  15. virtual context
  16. virtual patch
  17. virtual org
  18. virtual report
  19. //----------------------------------------------------------
  20. // For context mode
  21. //----------------------------------------------------------
  22. @depends on context@
  23. type T;
  24. @@
  25. * (T *)
  26. \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
  27. kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...)
  28. //----------------------------------------------------------
  29. // For patch mode
  30. //----------------------------------------------------------
  31. @depends on patch@
  32. type T;
  33. @@
  34. - (T *)
  35. (\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
  36. kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...))
  37. //----------------------------------------------------------
  38. // For org and report mode
  39. //----------------------------------------------------------
  40. @r depends on org || report@
  41. type T;
  42. position p;
  43. @@
  44. (T@p *)\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
  45. kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...)
  46. @script:python depends on org@
  47. p << r.p;
  48. t << r.T;
  49. @@
  50. coccilib.org.print_safe_todo(p[0], t)
  51. @script:python depends on report@
  52. p << r.p;
  53. t << r.T;
  54. @@
  55. msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
  56. coccilib.report.print_report(p[0], msg)