kstrdup.cocci 2.4 KB

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