diff options
author | Fabian Frederick <fabf@skynet.be> | 2014-08-08 17:23:29 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-08-08 18:57:26 -0400 |
commit | 791dfeb49519f18a6efea4619f5678457f1532ff (patch) | |
tree | d3f6d2e6e526eb6f6d382f5c9980abd8b002ea2c /scripts | |
parent | 56106da7932074804f9ef70191f126ea15dc3fc4 (diff) |
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>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/coccinelle/free/ifnullfree.cocci | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/scripts/coccinelle/free/ifnullfree.cocci b/scripts/coccinelle/free/ifnullfree.cocci new file mode 100644 index 000000000000..77ab43f30b8d --- /dev/null +++ b/scripts/coccinelle/free/ifnullfree.cocci | |||
@@ -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) | ||