aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/coccinelle.txt258
-rw-r--r--MAINTAINERS10
-rw-r--r--Makefile10
-rw-r--r--scripts/Makefile.help3
-rwxr-xr-xscripts/coccicheck54
-rw-r--r--scripts/coccinelle/alloc/drop_kmalloc_cast.cocci67
-rw-r--r--scripts/coccinelle/alloc/kzalloc-simple.cocci82
-rw-r--r--scripts/coccinelle/deref_null.cocci293
-rw-r--r--scripts/coccinelle/err_cast.cocci56
-rw-r--r--scripts/coccinelle/resource_size.cocci93
10 files changed, 923 insertions, 3 deletions
diff --git a/Documentation/coccinelle.txt b/Documentation/coccinelle.txt
new file mode 100644
index 000000000000..ba3315d545c6
--- /dev/null
+++ b/Documentation/coccinelle.txt
@@ -0,0 +1,258 @@
1Copyright 2010 Nicolas Palix <npalix@diku.dk>
2Copyright 2010 Julia Lawall <julia@diku.dk>
3Copyright 2010 Gilles Muller <Gilles.Muller@lip6.fr>
4
5
6 Getting Coccinelle
7~~~~~~~~~~~~~~~~~~~~
8
9The semantic patches included in the kernel use the 'virtual rule'
10feature which was introduced in Coccinelle version 0.1.11.
11
12Coccinelle (>=0.2.0) is available through the package manager
13of many distributions, e.g. :
14
15 - Debian (>=squeeze)
16 - Fedora (>=13)
17 - Ubuntu (>=10.04 Karmic Koala)
18 - OpenSUSE
19 - Arch Linux
20 - NetBSD
21 - FreeBSD
22
23
24You can get the latest version released from the Coccinelle homepage at
25http://coccinelle.lip6.fr/
26
27Once you have it, run the following command:
28
29 ./configure
30 make
31
32as a regular user, and install it with
33
34 sudo make install
35
36
37 Using Coccinelle on the Linux kernel
38~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39
40A Coccinelle-specific target is defined in the top level
41Makefile. This target is named 'coccicheck' and calls the 'coccicheck'
42front-end in the 'scripts' directory.
43
44Four modes are defined: report, patch, context, and org. The mode to
45use is specified by setting the MODE variable with 'MODE=<mode>'.
46
47'report' generates a list in the following format:
48 file:line:column-column: message
49
50'patch' proposes a fix, when possible.
51
52'context' highlights lines of interest and their context in a
53diff-like style.Lines of interest are indicated with '-'.
54
55'org' generates a report in the Org mode format of Emacs.
56
57Note that not all semantic patches implement all modes.
58
59To make a report for every semantic patch, run the following command:
60
61 make coccicheck MODE=report
62
63NB: The 'report' mode is the default one.
64
65To produce patches, run:
66
67 make coccicheck MODE=patch
68
69
70The coccicheck target applies every semantic patch available in the
71subdirectories of 'scripts/coccinelle' to the entire Linux kernel.
72
73For each semantic patch, a changelog message is proposed. It gives a
74description of the problem being checked by the semantic patch, and
75includes a reference to Coccinelle.
76
77As any static code analyzer, Coccinelle produces false
78positives. Thus, reports must be carefully checked, and patches
79reviewed.
80
81
82 Using Coccinelle with a single semantic patch
83~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84
85The optional make variable COCCI can be used to check a single
86semantic patch. In that case, the variable must be initialized with
87the name of the semantic patch to apply.
88
89For instance:
90
91 make coccicheck COCCI=<my_SP.cocci> MODE=patch
92or
93 make coccicheck COCCI=<my_SP.cocci> MODE=report
94
95
96 Proposing new semantic patches
97~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
99New semantic patches can be proposed and submitted by kernel
100developers. For sake of clarity, they should be organized in the
101subdirectories of 'scripts/coccinelle/'.
102
103
104 Detailed description of the 'report' mode
105~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106
107'report' generates a list in the following format:
108 file:line:column-column: message
109
110Example:
111
112Running
113
114 make coccicheck MODE=report COCCI=scripts/coccinelle/err_cast.cocci
115
116will execute the following part of the SmPL script.
117
118<smpl>
119@r depends on !context && !patch && (org || report)@
120expression x;
121position p;
122@@
123
124 ERR_PTR@p(PTR_ERR(x))
125
126@script:python depends on report@
127p << r.p;
128x << r.x;
129@@
130
131msg="ERR_CAST can be used with %s" % (x)
132coccilib.report.print_report(p[0], msg)
133</smpl>
134
135This SmPL excerpt generates entries on the standard output, as
136illustrated below:
137
138/home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
139/home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
140/home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
141
142
143 Detailed description of the 'patch' mode
144~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145
146When the 'patch' mode is available, it proposes a fix for each problem
147identified.
148
149Example:
150
151Running
152 make coccicheck MODE=patch COCCI=scripts/coccinelle/err_cast.cocci
153
154will execute the following part of the SmPL script.
155
156<smpl>
157@ depends on !context && patch && !org && !report @
158expression x;
159@@
160
161- ERR_PTR(PTR_ERR(x))
162+ ERR_CAST(x)
163</smpl>
164
165This SmPL excerpt generates patch hunks on the standard output, as
166illustrated below:
167
168diff -u -p a/crypto/ctr.c b/crypto/ctr.c
169--- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
170+++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
171@@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
172 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
173 CRYPTO_ALG_TYPE_MASK);
174 if (IS_ERR(alg))
175- return ERR_PTR(PTR_ERR(alg));
176+ return ERR_CAST(alg);
177
178 /* Block size must be >= 4 bytes. */
179 err = -EINVAL;
180
181 Detailed description of the 'context' mode
182~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
183
184'context' highlights lines of interest and their context
185in a diff-like style.
186
187NOTE: The diff-like output generated is NOT an applicable patch. The
188 intent of the 'context' mode is to highlight the important lines
189 (annotated with minus, '-') and gives some surrounding context
190 lines around. This output can be used with the diff mode of
191 Emacs to review the code.
192
193Example:
194
195Running
196 make coccicheck MODE=context COCCI=scripts/coccinelle/err_cast.cocci
197
198will execute the following part of the SmPL script.
199
200<smpl>
201@ depends on context && !patch && !org && !report@
202expression x;
203@@
204
205* ERR_PTR(PTR_ERR(x))
206</smpl>
207
208This SmPL excerpt generates diff hunks on the standard output, as
209illustrated below:
210
211diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
212--- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
213+++ /tmp/nothing
214@@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
215 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
216 CRYPTO_ALG_TYPE_MASK);
217 if (IS_ERR(alg))
218- return ERR_PTR(PTR_ERR(alg));
219
220 /* Block size must be >= 4 bytes. */
221 err = -EINVAL;
222
223 Detailed description of the 'org' mode
224~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
225
226'org' generates a report in the Org mode format of Emacs.
227
228Example:
229
230Running
231 make coccicheck MODE=org COCCI=scripts/coccinelle/err_cast.cocci
232
233will execute the following part of the SmPL script.
234
235<smpl>
236@r depends on !context && !patch && (org || report)@
237expression x;
238position p;
239@@
240
241 ERR_PTR@p(PTR_ERR(x))
242
243@script:python depends on org@
244p << r.p;
245x << r.x;
246@@
247
248msg="ERR_CAST can be used with %s" % (x)
249msg_safe=msg.replace("[","@(").replace("]",")")
250coccilib.org.print_todo(p[0], msg_safe)
251</smpl>
252
253This SmPL excerpt generates Org entries on the standard output, as
254illustrated below:
255
256* TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
257* TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
258* TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]
diff --git a/MAINTAINERS b/MAINTAINERS
index ce3601e5e3c8..0572b7ea5f9f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1476,6 +1476,16 @@ M: Daniel Oliveira Nascimento <don@syst.com.br>
1476S: Supported 1476S: Supported
1477F: drivers/platform/x86/classmate-laptop.c 1477F: drivers/platform/x86/classmate-laptop.c
1478 1478
1479COCCINELLE/Semantic Patches (SmPL)
1480M: Julia Lawall <julia@diku.dk>
1481M: Gilles Muller <Gilles.Muller@lip6.fr>
1482M: Nicolas Palix <npalix@diku.dk>
1483L: cocci@diku.dk (moderated for non-subscribers)
1484W: http://coccinelle.lip6.fr/
1485S: Supported
1486F: scripts/coccinelle/
1487F: scripts/coccicheck
1488
1479CODA FILE SYSTEM 1489CODA FILE SYSTEM
1480M: Jan Harkes <jaharkes@cs.cmu.edu> 1490M: Jan Harkes <jaharkes@cs.cmu.edu>
1481M: coda@cs.cmu.edu 1491M: coda@cs.cmu.edu
diff --git a/Makefile b/Makefile
index 46040e887d83..8acf7edad82c 100644
--- a/Makefile
+++ b/Makefile
@@ -412,7 +412,7 @@ endif
412# of make so .config is not included in this case either (for *config). 412# of make so .config is not included in this case either (for *config).
413 413
414no-dot-config-targets := clean mrproper distclean \ 414no-dot-config-targets := clean mrproper distclean \
415 cscope TAGS tags help %docs check% \ 415 cscope TAGS tags help %docs check% coccicheck \
416 include/linux/version.h headers_% \ 416 include/linux/version.h headers_% \
417 kernelrelease kernelversion 417 kernelrelease kernelversion
418 418
@@ -1279,8 +1279,9 @@ help:
1279 @echo ' includecheck - Check for duplicate included header files' 1279 @echo ' includecheck - Check for duplicate included header files'
1280 @echo ' export_report - List the usages of all exported symbols' 1280 @echo ' export_report - List the usages of all exported symbols'
1281 @echo ' headers_check - Sanity check on exported headers' 1281 @echo ' headers_check - Sanity check on exported headers'
1282 @echo ' headerdep - Detect inclusion cycles in headers'; \ 1282 @echo ' headerdep - Detect inclusion cycles in headers'
1283 echo '' 1283 @$(MAKE) -f $(srctree)/scripts/Makefile.help checker-help
1284 @echo ''
1284 @echo 'Kernel packaging:' 1285 @echo 'Kernel packaging:'
1285 @$(MAKE) $(build)=$(package-dir) help 1286 @$(MAKE) $(build)=$(package-dir) help
1286 @echo '' 1287 @echo ''
@@ -1439,6 +1440,9 @@ versioncheck:
1439 -name '*.[hcS]' -type f -print | sort \ 1440 -name '*.[hcS]' -type f -print | sort \
1440 | xargs $(PERL) -w $(srctree)/scripts/checkversion.pl 1441 | xargs $(PERL) -w $(srctree)/scripts/checkversion.pl
1441 1442
1443coccicheck:
1444 $(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
1445
1442namespacecheck: 1446namespacecheck:
1443 $(PERL) $(srctree)/scripts/namespace.pl 1447 $(PERL) $(srctree)/scripts/namespace.pl
1444 1448
diff --git a/scripts/Makefile.help b/scripts/Makefile.help
new file mode 100644
index 000000000000..d03608f5db04
--- /dev/null
+++ b/scripts/Makefile.help
@@ -0,0 +1,3 @@
1
2checker-help:
3 @echo ' coccicheck - Check with Coccinelle.'
diff --git a/scripts/coccicheck b/scripts/coccicheck
new file mode 100755
index 000000000000..037424b9ede5
--- /dev/null
+++ b/scripts/coccicheck
@@ -0,0 +1,54 @@
1#!/bin/sh
2
3SPATCH="`which ${SPATCH:=spatch}`"
4
5if [ ! -x "$SPATCH" ]; then
6 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
7 exit 1
8fi
9
10if [ "$MODE" = "" ] ; then
11 echo 'You have not explicitly specify the mode to use. Fallback to "report".'
12 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
13 echo 'Available modes are: report, patch, context, org'
14 MODE="report"
15fi
16
17echo ''
18echo 'Please check for false positives in the output before submitting a patch.'
19echo 'When using "patch" mode, carefully review the patch before submitting it.'
20echo ''
21
22function coccinelle {
23 COCCI="$1"
24 DIR="$2"
25
26 OPT=`grep "Option" $COCCI | cut -d':' -f2`
27 FILE=`echo $COCCI | sed "s|$DIR/||"`
28
29 echo "Processing `basename $COCCI` with option(s) \"$OPT\""
30 echo 'Message example to submit a patch:'
31
32 sed -e '/\/\/\//!d' -e 's|^///||' $COCCI
33
34 echo ' The semantic patch that makes this change is available'
35 echo " in $FILE."
36 echo ''
37 echo ' More information about semantic patching is available at'
38 echo ' http://coccinelle.lip6.fr/'
39 echo ''
40
41# The option '-parse_cocci' can be used to syntaxically check the SmPL files.
42#
43# $SPATCH -D $MODE -very_quiet -parse_cocci $COCCI $OPT > /dev/null
44
45 $SPATCH -D $MODE -very_quiet -sp_file $COCCI $OPT -dir $DIR
46}
47
48if [ "$COCCI" = "" ] ; then
49 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
50 coccinelle $f $srctree;
51 done
52else
53 coccinelle $COCCI $srctree
54fi
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)
diff --git a/scripts/coccinelle/deref_null.cocci b/scripts/coccinelle/deref_null.cocci
new file mode 100644
index 000000000000..9969d76d0f4b
--- /dev/null
+++ b/scripts/coccinelle/deref_null.cocci
@@ -0,0 +1,293 @@
1///
2/// A variable is dereference under a NULL test.
3/// Even though it is know to be NULL.
4///
5// Confidence: Moderate
6// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
7// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
8// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
9// URL: http://coccinelle.lip6.fr/
10// Comments: -I ... -all_includes can give more complete results
11// Options:
12
13virtual context
14virtual patch
15virtual org
16virtual report
17
18@initialize:python depends on !context && patch && !org && !report@
19
20import sys
21print >> sys.stderr, "This semantic patch does not support the 'patch' mode."
22
23@depends on patch@
24@@
25
26this_rule_should_never_matches();
27
28@ifm depends on !patch@
29expression *E;
30statement S1,S2;
31position p1;
32@@
33
34if@p1 ((E == NULL && ...) || ...) S1 else S2
35
36// The following two rules are separate, because both can match a single
37// expression in different ways
38@pr1 depends on !patch expression@
39expression *ifm.E;
40identifier f;
41position p1;
42@@
43
44 (E != NULL && ...) ? <+...E->f@p1...+> : ...
45
46@pr2 depends on !patch expression@
47expression *ifm.E;
48identifier f;
49position p2;
50@@
51
52(
53 (E != NULL) && ... && <+...E->f@p2...+>
54|
55 (E == NULL) || ... || <+...E->f@p2...+>
56|
57 sizeof(<+...E->f@p2...+>)
58)
59
60// For org and report modes
61
62@r depends on !context && !patch && (org || report) exists@
63expression subE <= ifm.E;
64expression *ifm.E;
65expression E1,E2;
66identifier f;
67statement S1,S2,S3,S4;
68iterator iter;
69position p!={pr1.p1,pr2.p2};
70position ifm.p1;
71@@
72
73if@p1 ((E == NULL && ...) || ...)
74{
75 ... when != if (...) S1 else S2
76(
77 iter(subE,...) S4 // no use
78|
79 list_remove_head(E2,subE,...)
80|
81 subE = E1
82|
83 for(subE = E1;...;...) S4
84|
85 subE++
86|
87 ++subE
88|
89 --subE
90|
91 subE--
92|
93 &subE
94|
95 E->f@p // bad use
96)
97 ... when any
98 return ...;
99}
100else S3
101
102@script:python depends on !context && !patch && !org && report@
103p << r.p;
104p1 << ifm.p1;
105x << ifm.E;
106@@
107
108msg="ERROR: %s is NULL but dereferenced." % (x)
109coccilib.report.print_report(p[0], msg)
110cocci.include_match(False)
111
112@script:python depends on !context && !patch && org && !report@
113p << r.p;
114p1 << ifm.p1;
115x << ifm.E;
116@@
117
118msg="ERROR: %s is NULL but dereferenced." % (x)
119msg_safe=msg.replace("[","@(").replace("]",")")
120cocci.print_main(msg_safe,p)
121cocci.include_match(False)
122
123@s depends on !context && !patch && (org || report) exists@
124expression subE <= ifm.E;
125expression *ifm.E;
126expression E1,E2;
127identifier f;
128statement S1,S2,S3,S4;
129iterator iter;
130position p!={pr1.p1,pr2.p2};
131position ifm.p1;
132@@
133
134if@p1 ((E == NULL && ...) || ...)
135{
136 ... when != if (...) S1 else S2
137(
138 iter(subE,...) S4 // no use
139|
140 list_remove_head(E2,subE,...)
141|
142 subE = E1
143|
144 for(subE = E1;...;...) S4
145|
146 subE++
147|
148 ++subE
149|
150 --subE
151|
152 subE--
153|
154 &subE
155|
156 E->f@p // bad use
157)
158 ... when any
159}
160else S3
161
162@script:python depends on !context && !patch && !org && report@
163p << s.p;
164p1 << ifm.p1;
165x << ifm.E;
166@@
167
168msg="ERROR: %s is NULL but dereferenced." % (x)
169coccilib.report.print_report(p[0], msg)
170
171@script:python depends on !context && !patch && org && !report@
172p << s.p;
173p1 << ifm.p1;
174x << ifm.E;
175@@
176
177msg="ERROR: %s is NULL but dereferenced." % (x)
178msg_safe=msg.replace("[","@(").replace("]",")")
179cocci.print_main(msg_safe,p)
180
181// For context mode
182
183@depends on context && !patch && !org && !report exists@
184expression subE <= ifm.E;
185expression *ifm.E;
186expression E1,E2;
187identifier f;
188statement S1,S2,S3,S4;
189iterator iter;
190position p!={pr1.p1,pr2.p2};
191position ifm.p1;
192@@
193
194if@p1 ((E == NULL && ...) || ...)
195{
196 ... when != if (...) S1 else S2
197(
198 iter(subE,...) S4 // no use
199|
200 list_remove_head(E2,subE,...)
201|
202 subE = E1
203|
204 for(subE = E1;...;...) S4
205|
206 subE++
207|
208 ++subE
209|
210 --subE
211|
212 subE--
213|
214 &subE
215|
216* E->f@p // bad use
217)
218 ... when any
219 return ...;
220}
221else S3
222
223// The following three rules are duplicates of ifm, pr1 and pr2 respectively.
224// It is need because the previous rule as already made a "change".
225
226@ifm1 depends on !patch@
227expression *E;
228statement S1,S2;
229position p1;
230@@
231
232if@p1 ((E == NULL && ...) || ...) S1 else S2
233
234@pr11 depends on !patch expression@
235expression *ifm1.E;
236identifier f;
237position p1;
238@@
239
240 (E != NULL && ...) ? <+...E->f@p1...+> : ...
241
242@pr12 depends on !patch expression@
243expression *ifm1.E;
244identifier f;
245position p2;
246@@
247
248(
249 (E != NULL) && ... && <+...E->f@p2...+>
250|
251 (E == NULL) || ... || <+...E->f@p2...+>
252|
253 sizeof(<+...E->f@p2...+>)
254)
255
256@depends on context && !patch && !org && !report exists@
257expression subE <= ifm1.E;
258expression *ifm1.E;
259expression E1,E2;
260identifier f;
261statement S1,S2,S3,S4;
262iterator iter;
263position p!={pr11.p1,pr12.p2};
264position ifm1.p1;
265@@
266
267if@p1 ((E == NULL && ...) || ...)
268{
269 ... when != if (...) S1 else S2
270(
271 iter(subE,...) S4 // no use
272|
273 list_remove_head(E2,subE,...)
274|
275 subE = E1
276|
277 for(subE = E1;...;...) S4
278|
279 subE++
280|
281 ++subE
282|
283 --subE
284|
285 subE--
286|
287 &subE
288|
289* E->f@p // bad use
290)
291 ... when any
292}
293else S3
diff --git a/scripts/coccinelle/err_cast.cocci b/scripts/coccinelle/err_cast.cocci
new file mode 100644
index 000000000000..2ce115000af6
--- /dev/null
+++ b/scripts/coccinelle/err_cast.cocci
@@ -0,0 +1,56 @@
1///
2/// Use ERR_CAST inlined function instead of ERR_PTR(PTR_ERR(...))
3///
4// Confidence: High
5// Copyright: (C) 2009, 2010 Nicolas Palix, DIKU. GPLv2.
6// Copyright: (C) 2009, 2010 Julia Lawall, DIKU. GPLv2.
7// Copyright: (C) 2009, 2010 Gilles Muller, INRIA/LiP6. GPLv2.
8// URL: http://coccinelle.lip6.fr/
9// Options:
10//
11// Keywords: ERR_PTR, PTR_ERR, ERR_CAST
12// Version min: 2.6.25
13//
14
15virtual context
16virtual patch
17virtual org
18virtual report
19
20
21@ depends on context && !patch && !org && !report@
22expression x;
23@@
24
25* ERR_PTR(PTR_ERR(x))
26
27@ depends on !context && patch && !org && !report @
28expression x;
29@@
30
31- ERR_PTR(PTR_ERR(x))
32+ ERR_CAST(x)
33
34@r depends on !context && !patch && (org || report)@
35expression x;
36position p;
37@@
38
39 ERR_PTR@p(PTR_ERR(x))
40
41@script:python depends on org@
42p << r.p;
43x << r.x;
44@@
45
46msg="WARNING ERR_CAST can be used with %s" % (x)
47msg_safe=msg.replace("[","@(").replace("]",")")
48coccilib.org.print_todo(p[0], msg_safe)
49
50@script:python depends on report@
51p << r.p;
52x << r.x;
53@@
54
55msg="WARNING: ERR_CAST can be used with %s" % (x)
56coccilib.report.print_report(p[0], msg)
diff --git a/scripts/coccinelle/resource_size.cocci b/scripts/coccinelle/resource_size.cocci
new file mode 100644
index 000000000000..1935a58b39d9
--- /dev/null
+++ b/scripts/coccinelle/resource_size.cocci
@@ -0,0 +1,93 @@
1///
2/// Use resource_size function on resource object
3/// instead of explicit computation.
4///
5// Confidence: High
6// Copyright: (C) 2009, 2010 Nicolas Palix, DIKU. GPLv2.
7// Copyright: (C) 2009, 2010 Julia Lawall, DIKU. GPLv2.
8// Copyright: (C) 2009, 2010 Gilles Muller, INRIA/LiP6. GPLv2.
9// URL: http://coccinelle.lip6.fr/
10// Options:
11//
12// Keywords: resource_size
13// Version min: 2.6.27 resource_size
14//
15
16virtual context
17virtual patch
18virtual org
19virtual report
20
21//----------------------------------------------------------
22// For context mode
23//----------------------------------------------------------
24
25@r_context depends on context && !patch && !org@
26struct resource *res;
27@@
28
29* (res->end - res->start) + 1
30
31//----------------------------------------------------------
32// For patch mode
33//----------------------------------------------------------
34
35@r_patch depends on !context && patch && !org@
36struct resource *res;
37@@
38
39- (res->end - res->start) + 1
40+ resource_size(res)
41
42//----------------------------------------------------------
43// For org mode
44//----------------------------------------------------------
45
46
47@r_org depends on !context && !patch && (org || report)@
48struct resource *res;
49position p;
50@@
51
52 (res->end@p - res->start) + 1
53
54@rbad_org depends on !context && !patch && (org || report)@
55struct resource *res;
56position p != r_org.p;
57@@
58
59 res->end@p - res->start
60
61@script:python depends on org@
62p << r_org.p;
63x << r_org.res;
64@@
65
66msg="ERROR with %s" % (x)
67msg_safe=msg.replace("[","@(").replace("]",")")
68coccilib.org.print_todo(p[0], msg_safe)
69
70@script:python depends on report@
71p << r_org.p;
72x << r_org.res;
73@@
74
75msg="ERROR: Missing resource_size with %s" % (x)
76coccilib.report.print_report(p[0], msg)
77
78@script:python depends on org@
79p << rbad_org.p;
80x << rbad_org.res;
81@@
82
83msg="WARNING with %s" % (x)
84msg_safe=msg.replace("[","@(").replace("]",")")
85coccilib.org.print_todo(p[0], msg_safe)
86
87@script:python depends on report@
88p << rbad_org.p;
89x << rbad_org.res;
90@@
91
92msg="WARNING: Suspicious code. resource_size is maybe missing with %s" % (x)
93coccilib.report.print_report(p[0], msg)