memdup.cocci 1.4 KB

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