0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /// Use kmemdup rather than duplicating its implementation
0003 ///
0004 // Confidence: High
0005 // Copyright: (C) 2010-2012 Nicolas Palix.
0006 // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
0007 // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
0008 // URL: https://coccinelle.gitlabpages.inria.fr/website
0009 // Comments:
0010 // Options: --no-includes --include-headers
0011
0012 virtual patch
0013 virtual context
0014 virtual org
0015 virtual report
0016
0017 @r1@
0018 expression from,to;
0019 expression flag;
0020 position p;
0021 @@
0022
0023 to = \(kmalloc@p\|kzalloc@p\)(strlen(from) + 1,flag);
0024
0025 @r2@
0026 expression x,from,to;
0027 expression flag,E1;
0028 position p;
0029 @@
0030
0031 x = strlen(from) + 1;
0032 ... when != \( x = E1 \| from = E1 \)
0033 to = \(kmalloc@p\|kzalloc@p\)(x,flag);
0034
0035 @depends on patch@
0036 expression from,to,size,flag;
0037 position p != {r1.p,r2.p};
0038 statement S;
0039 @@
0040
0041 - to = \(kmalloc@p\|kzalloc@p\)(size,flag);
0042 + to = kmemdup(from,size,flag);
0043 if (to==NULL || ...) S
0044 - memcpy(to, from, size);
0045
0046 @r depends on !patch@
0047 expression from,to,size,flag;
0048 position p != {r1.p,r2.p};
0049 statement S;
0050 @@
0051
0052 * to = \(kmalloc@p\|kzalloc@p\)(size,flag);
0053 if (to==NULL || ...) S
0054 * memcpy(to, from, size);
0055
0056 @script:python depends on org@
0057 p << r.p;
0058 @@
0059
0060 coccilib.org.print_todo(p[0], "WARNING opportunity for kmemdup")
0061
0062 @script:python depends on report@
0063 p << r.p;
0064 @@
0065
0066 coccilib.report.print_report(p[0], "WARNING opportunity for kmemdup")