aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/coccinelle/alloc
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/coccinelle/alloc')
-rw-r--r--scripts/coccinelle/alloc/drop_kmalloc_cast.cocci67
-rw-r--r--scripts/coccinelle/alloc/kzalloc-simple.cocci82
2 files changed, 149 insertions, 0 deletions
diff --git a/scripts/coccinelle/alloc/drop_kmalloc_cast.cocci b/scripts/coccinelle/alloc/drop_kmalloc_cast.cocci
new file mode 100644
index 000000000000..7d4771d449c3
--- /dev/null
+++ b/scripts/coccinelle/alloc/drop_kmalloc_cast.cocci
@@ -0,0 +1,67 @@
1///
2/// Casting (void *) value returned by kmalloc is useless
3/// as mentioned in Documentation/CodingStyle, Chap 14.
4///
5// Confidence: High
6// Copyright: 2009,2010 Nicolas Palix, DIKU. GPLv2.
7// URL: http://coccinelle.lip6.fr/
8// Options: -no_includes -include_headers
9//
10// Keywords: kmalloc, kzalloc, kcalloc
11// Version min: < 2.6.12 kmalloc
12// Version min: < 2.6.12 kcalloc
13// Version min: 2.6.14 kzalloc
14//
15
16virtual context
17virtual patch
18virtual org
19virtual report
20
21//----------------------------------------------------------
22// For context mode
23//----------------------------------------------------------
24
25@depends on context@
26type T;
27@@
28
29* (T *)
30 \(kmalloc\|kzalloc\|kcalloc\)(...)
31
32//----------------------------------------------------------
33// For patch mode
34//----------------------------------------------------------
35
36@depends on patch@
37type T;
38@@
39
40- (T *)
41 \(kmalloc\|kzalloc\|kcalloc\)(...)
42
43//----------------------------------------------------------
44// For org and report mode
45//----------------------------------------------------------
46
47@r depends on org || report@
48type T;
49position p;
50@@
51
52 (T@p *)\(kmalloc\|kzalloc\|kcalloc\)(...)
53
54@script:python depends on org@
55p << r.p;
56t << r.T;
57@@
58
59coccilib.org.print_safe_todo(p[0], t)
60
61@script:python depends on report@
62p << r.p;
63t << r.T;
64@@
65
66msg="WARNING: casting value returned by k[cmz]alloc to (%s *) is useless." % (t)
67coccilib.report.print_report(p[0], msg)
diff --git a/scripts/coccinelle/alloc/kzalloc-simple.cocci b/scripts/coccinelle/alloc/kzalloc-simple.cocci
new file mode 100644
index 000000000000..2eae828fc657
--- /dev/null
+++ b/scripts/coccinelle/alloc/kzalloc-simple.cocci
@@ -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
15virtual context
16virtual patch
17virtual org
18virtual report
19
20//----------------------------------------------------------
21// For context mode
22//----------------------------------------------------------
23
24@depends on context@
25type T, T2;
26expression x;
27expression E1,E2;
28statement 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@
40type T, T2;
41expression x;
42expression E1,E2;
43statement 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@
56type T, T2;
57expression x;
58expression E1,E2;
59statement S;
60position 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@
68p << r.p;
69x << r.x;
70@@
71
72msg="%s" % (x)
73msg_safe=msg.replace("[","@(").replace("]",")")
74coccilib.org.print_todo(p[0], msg_safe)
75
76@script:python depends on report@
77p << r.p;
78x << r.x;
79@@
80
81msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
82coccilib.report.print_report(p[0], msg)