memdup.cocci 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /// Use kmemdup 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. @r1@
  16. expression from,to;
  17. expression flag;
  18. position p;
  19. @@
  20. to = \(kmalloc@p\|kzalloc@p\)(strlen(from) + 1,flag);
  21. @r2@
  22. expression x,from,to;
  23. expression flag,E1;
  24. position p;
  25. @@
  26. x = strlen(from) + 1;
  27. ... when != \( x = E1 \| from = E1 \)
  28. to = \(kmalloc@p\|kzalloc@p\)(x,flag);
  29. @depends on patch@
  30. expression from,to,size,flag;
  31. position p != {r1.p,r2.p};
  32. statement S;
  33. @@
  34. - to = \(kmalloc@p\|kzalloc@p\)(size,flag);
  35. + to = kmemdup(from,size,flag);
  36. if (to==NULL || ...) S
  37. - memcpy(to, from, size);
  38. @r depends on !patch@
  39. expression from,to,size,flag;
  40. position p != {r1.p,r2.p};
  41. statement S;
  42. @@
  43. * to = \(kmalloc@p\|kzalloc@p\)(size,flag);
  44. if (to==NULL || ...) S
  45. * memcpy(to, from, size);
  46. @script:python depends on org@
  47. p << r.p;
  48. @@
  49. coccilib.org.print_todo(p[0], "WARNING opportunity for kmemdup")
  50. @script:python depends on report@
  51. p << r.p;
  52. @@
  53. coccilib.report.print_report(p[0], "WARNING opportunity for kmemdup")