memdup.cocci 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if (to==NULL || ...) S
  44. * memcpy(to, from, size);
  45. @script:python depends on org@
  46. p << r.p;
  47. @@
  48. coccilib.org.print_todo(p[0], "WARNING opportunity for kmemdup")
  49. @script:python depends on report@
  50. p << r.p;
  51. @@
  52. coccilib.report.print_report(p[0], "WARNING opportunity for kmemdup")