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

Showing the changes to scripts (1 of 1 file). Show all

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
10virtual patch
11virtual org
12virtual report
13virtual context
14
15@r2 depends on patch@
16expression 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 @
34expression E;
35position 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@
42p << r.p;
43@@
44
45cocci.print_main("NULL check before that freeing function is not needed", p)
46
47@script:python depends on report@
48p << r.p;
49@@
50
51msg = "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."
52coccilib.report.print_report(p[0], msg)