^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 kstrdup 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) @depends on patch@
^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,E1,E2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) statement S;
^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(strlen(from) + 1,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) + to = kstrdup(from, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ... when != \(from = E1 \| to = E1 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (to==NULL || ...) S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ... when != \(from = E2 \| to = E2 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) - strcpy(to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) @depends on patch@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) expression x,from,to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) expression flag,E1,E2,E3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) statement S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) - x = strlen(from) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ... when != \( x = E1 \| from = E1 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) - to = \(kmalloc\|kzalloc\)(x,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) + to = kstrdup(from, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ... when != \(x = E2 \| from = E2 \| to = E2 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (to==NULL || ...) S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ... when != \(x = E3 \| from = E3 \| to = E3 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) - memcpy(to, from, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) // ---------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) @r1 depends on !patch exists@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) expression from,to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) expression flag,E1,E2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) statement S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) position p1,p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * to = kmalloc@p1(strlen(from) + 1,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ... when != \(from = E1 \| to = E1 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (to==NULL || ...) S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ... when != \(from = E2 \| to = E2 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * strcpy@p2(to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) @r2 depends on !patch exists@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) expression x,from,to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) expression flag,E1,E2,E3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) statement S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) position p1,p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * x = strlen(from) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ... when != \( x = E1 \| from = E1 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * to = \(kmalloc@p1\|kzalloc@p1\)(x,flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ... when != \(x = E2 \| from = E2 \| to = E2 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (to==NULL || ...) S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ... when != \(x = E3 \| from = E3 \| to = E3 \)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * memcpy@p2(to, from, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) @script:python depends on org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) p1 << r1.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) p2 << r1.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) cocci.print_main("WARNING opportunity for kstrdup",p1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) cocci.print_secs("strcpy",p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) @script:python depends on org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) p1 << r2.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) p2 << r2.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) cocci.print_main("WARNING opportunity for kstrdup",p1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) cocci.print_secs("memcpy",p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) @script:python depends on report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) p1 << r1.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) p2 << r1.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) msg = "WARNING opportunity for kstrdup (strcpy on line %s)" % (p2[0].line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) coccilib.report.print_report(p1[0], msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) @script:python depends on report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) p1 << r2.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) p2 << r2.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) msg = "WARNING opportunity for kstrdup (memcpy on line %s)" % (p2[0].line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) coccilib.report.print_report(p1[0], msg)