kzalloc-simple.cocci 1.9 KB

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