Skip to content

Commit 791dfeb

Browse files
Fabian Fredericktorvalds
authored andcommitted
scripts/coccinelle/free: add NULL test before freeing functions
Warns or generates patch for NULL check before the following functions: kfree usb_free_urb debugfs_remove debugfs_remove_recursive Signed-off-by: Fabian Frederick <fabf@skynet.be> Acked-by: Julia Lawall <Julia.Lawall@lip6.fr> Cc: Gilles Muller <Gilles.Muller@lip6.fr> Cc: Joe Perches <joe@perches.com> Cc: Markus Elfring <elfring@users.sourceforge.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 56106da commit 791dfeb

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// NULL check before some freeing functions is not needed.
2+
///
3+
/// Based on checkpatch warning
4+
/// "kfree(NULL) is safe this check is probably not required"
5+
/// and kfreeaddr.cocci by Julia Lawall.
6+
///
7+
/// Comments: -
8+
/// Options: --no-includes --include-headers
9+
10+
virtual patch
11+
virtual org
12+
virtual report
13+
virtual context
14+
15+
@r2 depends on patch@
16+
expression E;
17+
@@
18+
- if (E)
19+
(
20+
- kfree(E);
21+
+ kfree(E);
22+
|
23+
- debugfs_remove(E);
24+
+ debugfs_remove(E);
25+
|
26+
- debugfs_remove_recursive(E);
27+
+ debugfs_remove_recursive(E);
28+
|
29+
- usb_free_urb(E);
30+
+ usb_free_urb(E);
31+
)
32+
33+
@r depends on context || report || org @
34+
expression E;
35+
position p;
36+
@@
37+
38+
* if (E)
39+
* \(kfree@p\|debugfs_remove@p\|debugfs_remove_recursive@p\|usb_free_urb\)(E);
40+
41+
@script:python depends on org@
42+
p << r.p;
43+
@@
44+
45+
cocci.print_main("NULL check before that freeing function is not needed", p)
46+
47+
@script:python depends on report@
48+
p << r.p;
49+
@@
50+
51+
msg = "WARNING: NULL check before freeing functions like kfree, debugfs_remove, debugfs_remove_recursive or usb_free_urb is not needed. Maybe consider reorganizing relevant code to avoid passing NULL values."
52+
coccilib.report.print_report(p[0], msg)

0 commit comments

Comments
 (0)