This semantic patch replaces a pair of calls to kmalloc and memset by a single call to kzalloc. It only looks for simple cases to avoid false positives.
Showing the changes to scripts (1 of 1 file). Show all
Unified · Split
| @@ -0,0 +1,82 @@ | ||
| 1 | +/// | |
| 2 | +/// kzalloc should be used rather than kmalloc followed by memset 0 | |
| 3 | +/// | |
| 4 | +// Confidence: High | |
| 5 | +// Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU. GPLv2. | |
| 6 | +// Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6. GPLv2. | |
| 7 | +// URL: http://coccinelle.lip6.fr/rules/kzalloc.html | |
| 8 | +// Options: -no_includes -include_headers | |
| 9 | +// | |
| 10 | +// Keywords: kmalloc, kzalloc | |
| 11 | +// Version min: < 2.6.12 kmalloc | |
| 12 | +// Version min: 2.6.14 kzalloc | |
| 13 | +// | |
| 14 | + | |
| 15 | +virtual context | |
| 16 | +virtual patch | |
| 17 | +virtual org | |
| 18 | +virtual report | |
| 19 | + | |
| 20 | +//---------------------------------------------------------- | |
| 21 | +// For context mode | |
| 22 | +//---------------------------------------------------------- | |
| 23 | + | |
| 24 | +@depends on context@ | |
| 25 | +type T, T2; | |
| 26 | +expression x; | |
| 27 | +expression E1,E2; | |
| 28 | +statement S; | |
| 29 | +@@ | |
| 30 | + | |
| 31 | +* x = (T)kmalloc(E1,E2); | |
| 32 | + if ((x==NULL) || ...) S | |
| 33 | +* memset((T2)x,0,E1); | |
| 34 | + | |
| 35 | +//---------------------------------------------------------- | |
| 36 | +// For patch mode | |
| 37 | +//---------------------------------------------------------- | |
| 38 | + | |
| 39 | +@depends on patch@ | |
| 40 | +type T, T2; | |
| 41 | +expression x; | |
| 42 | +expression E1,E2; | |
| 43 | +statement S; | |
| 44 | +@@ | |
| 45 | + | |
| 46 | +- x = (T)kmalloc(E1,E2); | |
| 47 | ++ x = kzalloc(E1,E2); | |
| 48 | + if ((x==NULL) || ...) S | |
| 49 | +- memset((T2)x,0,E1); | |
| 50 | + | |
| 51 | +//---------------------------------------------------------- | |
| 52 | +// For org mode | |
| 53 | +//---------------------------------------------------------- | |
| 54 | + | |
| 55 | +@r depends on org || report@ | |
| 56 | +type T, T2; | |
| 57 | +expression x; | |
| 58 | +expression E1,E2; | |
| 59 | +statement S; | |
| 60 | +position p; | |
| 61 | +@@ | |
| 62 | + | |
| 63 | + x = (T)kmalloc@p(E1,E2); | |
| 64 | + if ((x==NULL) || ...) S | |
| 65 | + memset((T2)x,0,E1); | |
| 66 | + | |
| 67 | +@script:python depends on org@ | |
| 68 | +p << r.p; | |
| 69 | +x << r.x; | |
| 70 | +@@ | |
| 71 | + | |
| 72 | +msg="%s" % (x) | |
| 73 | +msg_safe=msg.replace("[","@(").replace("]",")") | |
| 74 | +coccilib.org.print_todo(p[0], msg_safe) | |
| 75 | + | |
| 76 | +@script:python depends on report@ | |
| 77 | +p << r.p; | |
| 78 | +x << r.x; | |
| 79 | +@@ | |
| 80 | + | |
| 81 | +msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x) | |
| 82 | +coccilib.report.print_report(p[0], msg) | |