^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /// Use kmemdup rather than duplicating its implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ///
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) // Confidence: High
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright: (C) 2010-2012 Nicolas Palix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) // URL: http://coccinelle.lip6.fr/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) // Comments:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) // Options: --no-includes --include-headers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) virtual patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) virtual context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) virtual org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) virtual report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) @r1@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) expression from,to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) expression flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) position p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) to = \(kmalloc@p\|kzalloc@p\)(strlen(from) + 1,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) @r2@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) expression x,from,to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) expression flag,E1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) position p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) x = strlen(from) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ... when != \( x = E1 \| from = E1 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) to = \(kmalloc@p\|kzalloc@p\)(x,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) @depends on patch@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) expression from,to,size,flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) position p != {r1.p,r2.p};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) statement S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) - to = \(kmalloc@p\|kzalloc@p\)(size,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) + to = kmemdup(from,size,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (to==NULL || ...) S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) - memcpy(to, from, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) @r depends on !patch@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) expression from,to,size,flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) position p != {r1.p,r2.p};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) statement S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * to = \(kmalloc@p\|kzalloc@p\)(size,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (to==NULL || ...) S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * memcpy(to, from, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) @script:python depends on org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) p << r.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) coccilib.org.print_todo(p[0], "WARNING opportunity for kmemdup")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) @script:python depends on report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) p << r.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) coccilib.report.print_report(p[0], "WARNING opportunity for kmemdup")