kstrdup.cocci 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /// Use kstrdup rather than duplicating its implementation
  2. ///
  3. // Confidence: High
  4. // Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
  5. // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
  6. // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
  7. // URL: http://coccinelle.lip6.fr/
  8. // Comments:
  9. // Options: --no-includes --include-headers
  10. virtual patch
  11. virtual context
  12. virtual org
  13. virtual report
  14. @depends on patch@
  15. expression from,to;
  16. expression flag,E1,E2;
  17. statement S;
  18. @@
  19. - to = kmalloc(strlen(from) + 1,flag);
  20. + to = kstrdup(from, flag);
  21. ... when != \(from = E1 \| to = E1 \)
  22. if (to==NULL || ...) S
  23. ... when != \(from = E2 \| to = E2 \)
  24. - strcpy(to, from);
  25. @depends on patch@
  26. expression x,from,to;
  27. expression flag,E1,E2,E3;
  28. statement S;
  29. @@
  30. - x = strlen(from) + 1;
  31. ... when != \( x = E1 \| from = E1 \)
  32. - to = \(kmalloc\|kzalloc\)(x,flag);
  33. + to = kstrdup(from, flag);
  34. ... when != \(x = E2 \| from = E2 \| to = E2 \)
  35. if (to==NULL || ...) S
  36. ... when != \(x = E3 \| from = E3 \| to = E3 \)
  37. - memcpy(to, from, x);
  38. // ---------------------------------------------------------------------
  39. @r1 depends on !patch exists@
  40. expression from,to;
  41. expression flag,E1,E2;
  42. statement S;
  43. position p1,p2;
  44. @@
  45. * to = kmalloc@p1(strlen(from) + 1,flag);
  46. ... when != \(from = E1 \| to = E1 \)
  47. if (to==NULL || ...) S
  48. ... when != \(from = E2 \| to = E2 \)
  49. * strcpy@p2(to, from);
  50. @r2 depends on !patch exists@
  51. expression x,from,to;
  52. expression flag,E1,E2,E3;
  53. statement S;
  54. position p1,p2;
  55. @@
  56. * x = strlen(from) + 1;
  57. ... when != \( x = E1 \| from = E1 \)
  58. * to = \(kmalloc@p1\|kzalloc@p2\)(x,flag);
  59. ... when != \(x = E2 \| from = E2 \| to = E2 \)
  60. if (to==NULL || ...) S
  61. ... when != \(x = E3 \| from = E3 \| to = E3 \)
  62. * memcpy@p2(to, from, x);
  63. @script:python depends on org@
  64. p1 << r1.p1;
  65. p2 << r1.p2;
  66. @@
  67. cocci.print_main("WARNING opportunity for kstrdep",p1)
  68. cocci.print_secs("strcpy",p2)
  69. @script:python depends on org@
  70. p1 << r2.p1;
  71. p2 << r2.p2;
  72. @@
  73. cocci.print_main("WARNING opportunity for kstrdep",p1)
  74. cocci.print_secs("memcpy",p2)
  75. @script:python depends on report@
  76. p1 << r1.p1;
  77. p2 << r1.p2;
  78. @@
  79. msg = "WARNING opportunity for kstrdep (strcpy on line %s)" % (p2[0].line)
  80. coccilib.report.print_report(p1[0], msg)
  81. @script:python depends on report@
  82. p1 << r2.p1;
  83. p2 << r2.p2;
  84. @@
  85. msg = "WARNING opportunity for kstrdep (memcpy on line %s)" % (p2[0].line)
  86. coccilib.report.print_report(p1[0], msg)