memdup.cocci 849 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /// Use kmemdup rather than duplicating its implementation
  2. ///
  3. // Confidence: High
  4. // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
  5. // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
  6. // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
  7. // URL: http://coccinelle.lip6.fr/
  8. // Comments:
  9. // Options: -no_includes -include_headers
  10. virtual patch
  11. @r1@
  12. expression from,to;
  13. expression flag;
  14. position p;
  15. @@
  16. to = \(kmalloc@p\|kzalloc@p\)(strlen(from) + 1,flag);
  17. @r2@
  18. expression x,from,to;
  19. expression flag,E1;
  20. position p;
  21. @@
  22. x = strlen(from) + 1;
  23. ... when != \( x = E1 \| from = E1 \)
  24. to = \(kmalloc@p\|kzalloc@p\)(x,flag);
  25. @@
  26. expression from,to,size,flag;
  27. position p != {r1.p,r2.p};
  28. statement S;
  29. @@
  30. - to = \(kmalloc@p\|kzalloc@p\)(size,flag);
  31. + to = kmemdup(from,size,flag);
  32. if (to==NULL || ...) S
  33. - memcpy(to, from, size);