History

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

Fabian Frederick committed 12 years ago

Commit 791dfeb1 parent 56106da1 file · +52 −0Browse files

Unified · Split

scripts/coccinelle/free/ifnullfree.cocci+52−0
@@ -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)