kzalloc-simple.cocci 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ///
  2. /// kzalloc should be used rather than kmalloc followed by memset 0
  3. ///
  4. // Confidence: High
  5. // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU. GPLv2.
  6. // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6. GPLv2.
  7. // URL: http://coccinelle.lip6.fr/rules/kzalloc.html
  8. // Options: -no_includes -include_headers
  9. //
  10. // Keywords: kmalloc, kzalloc
  11. // Version min: < 2.6.12 kmalloc
  12. // Version min: 2.6.14 kzalloc
  13. //
  14. virtual context
  15. virtual patch
  16. virtual org
  17. virtual report
  18. //----------------------------------------------------------
  19. // For context mode
  20. //----------------------------------------------------------
  21. @depends on context@
  22. type T, T2;
  23. expression x;
  24. expression E1,E2;
  25. statement S;
  26. @@
  27. * x = (T)kmalloc(E1,E2);
  28. if ((x==NULL) || ...) S
  29. * memset((T2)x,0,E1);
  30. //----------------------------------------------------------
  31. // For patch mode
  32. //----------------------------------------------------------
  33. @depends on patch@
  34. type T, T2;
  35. expression x;
  36. expression E1,E2;
  37. statement S;
  38. @@
  39. - x = (T)kmalloc(E1,E2);
  40. + x = kzalloc(E1,E2);
  41. if ((x==NULL) || ...) S
  42. - memset((T2)x,0,E1);
  43. //----------------------------------------------------------
  44. // For org mode
  45. //----------------------------------------------------------
  46. @r depends on org || report@
  47. type T, T2;
  48. expression x;
  49. expression E1,E2;
  50. statement S;
  51. position p;
  52. @@
  53. x = (T)kmalloc@p(E1,E2);
  54. if ((x==NULL) || ...) S
  55. memset((T2)x,0,E1);
  56. @script:python depends on org@
  57. p << r.p;
  58. x << r.x;
  59. @@
  60. msg="%s" % (x)
  61. msg_safe=msg.replace("[","@(").replace("]",")")
  62. coccilib.org.print_todo(p[0], msg_safe)
  63. @script:python depends on report@
  64. p << r.p;
  65. x << r.x;
  66. @@
  67. msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
  68. coccilib.report.print_report(p[0], msg)