xref: /linux/scripts/coccinelle/api/memdup.cocci (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
17f904d7eSThomas Gleixner// SPDX-License-Identifier: GPL-2.0-only
20426a97cSNicolas Palix/// Use kmemdup rather than duplicating its implementation
30426a97cSNicolas Palix///
40426a97cSNicolas Palix// Confidence: High
57f904d7eSThomas Gleixner// Copyright: (C) 2010-2012 Nicolas Palix.
67f904d7eSThomas Gleixner// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
77f904d7eSThomas Gleixner// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
8*f01701ceSJulia Lawall// URL: https://coccinelle.gitlabpages.inria.fr/website
90426a97cSNicolas Palix// Comments:
1093f14468SNicolas Palix// Options: --no-includes --include-headers
110426a97cSNicolas Palix
120426a97cSNicolas Palixvirtual patch
1329a36d4dSJulia Lawallvirtual context
1429a36d4dSJulia Lawallvirtual org
1529a36d4dSJulia Lawallvirtual report
160426a97cSNicolas Palix
170426a97cSNicolas Palix@r1@
180426a97cSNicolas Palixexpression from,to;
190426a97cSNicolas Palixexpression flag;
200426a97cSNicolas Palixposition p;
210426a97cSNicolas Palix@@
220426a97cSNicolas Palix
230426a97cSNicolas Palix   to = \(kmalloc@p\|kzalloc@p\)(strlen(from) + 1,flag);
240426a97cSNicolas Palix
250426a97cSNicolas Palix@r2@
260426a97cSNicolas Palixexpression x,from,to;
270426a97cSNicolas Palixexpression flag,E1;
280426a97cSNicolas Palixposition p;
290426a97cSNicolas Palix@@
300426a97cSNicolas Palix
310426a97cSNicolas Palix    x = strlen(from) + 1;
320426a97cSNicolas Palix    ... when != \( x = E1 \| from = E1 \)
330426a97cSNicolas Palix    to = \(kmalloc@p\|kzalloc@p\)(x,flag);
340426a97cSNicolas Palix
3529a36d4dSJulia Lawall@depends on patch@
360426a97cSNicolas Palixexpression from,to,size,flag;
370426a97cSNicolas Palixposition p != {r1.p,r2.p};
380426a97cSNicolas Palixstatement S;
390426a97cSNicolas Palix@@
400426a97cSNicolas Palix
410426a97cSNicolas Palix-  to = \(kmalloc@p\|kzalloc@p\)(size,flag);
420426a97cSNicolas Palix+  to = kmemdup(from,size,flag);
430426a97cSNicolas Palix   if (to==NULL || ...) S
440426a97cSNicolas Palix-  memcpy(to, from, size);
4529a36d4dSJulia Lawall
4629a36d4dSJulia Lawall@r depends on !patch@
4729a36d4dSJulia Lawallexpression from,to,size,flag;
4829a36d4dSJulia Lawallposition p != {r1.p,r2.p};
4929a36d4dSJulia Lawallstatement S;
5029a36d4dSJulia Lawall@@
5129a36d4dSJulia Lawall
5229a36d4dSJulia Lawall*  to = \(kmalloc@p\|kzalloc@p\)(size,flag);
5329a36d4dSJulia Lawall   if (to==NULL || ...) S
5429a36d4dSJulia Lawall*  memcpy(to, from, size);
5529a36d4dSJulia Lawall
5629a36d4dSJulia Lawall@script:python depends on org@
5729a36d4dSJulia Lawallp << r.p;
5829a36d4dSJulia Lawall@@
5929a36d4dSJulia Lawall
60a11761c2SDafna Hirschfeldcoccilib.org.print_todo(p[0], "WARNING opportunity for kmemdup")
6129a36d4dSJulia Lawall
6229a36d4dSJulia Lawall@script:python depends on report@
6329a36d4dSJulia Lawallp << r.p;
6429a36d4dSJulia Lawall@@
6529a36d4dSJulia Lawall
66a11761c2SDafna Hirschfeldcoccilib.report.print_report(p[0], "WARNING opportunity for kmemdup")
67