kstrdup.cocci 973 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /// Use kstrdup 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. @@
  12. expression from,to;
  13. expression flag,E1,E2;
  14. statement S;
  15. @@
  16. - to = kmalloc(strlen(from) + 1,flag);
  17. + to = kstrdup(from, flag);
  18. ... when != \(from = E1 \| to = E1 \)
  19. if (to==NULL || ...) S
  20. ... when != \(from = E2 \| to = E2 \)
  21. - strcpy(to, from);
  22. @@
  23. expression x,from,to;
  24. expression flag,E1,E2,E3;
  25. statement S;
  26. @@
  27. - x = strlen(from) + 1;
  28. ... when != \( x = E1 \| from = E1 \)
  29. - to = \(kmalloc\|kzalloc\)(x,flag);
  30. + to = kstrdup(from, flag);
  31. ... when != \(x = E2 \| from = E2 \| to = E2 \)
  32. if (to==NULL || ...) S
  33. ... when != \(x = E3 \| from = E3 \| to = E3 \)
  34. - memcpy(to, from, x);