Merge tag 'gfs2-4.8.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2...
[cascardo/linux.git] / scripts / coccinelle / free / ifnullfree.cocci
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 // Copyright: (C) 2014 Fabian Frederick.  GPLv2.
8 // Comments: -
9 // Options: --no-includes --include-headers
10
11 virtual patch
12 virtual org
13 virtual report
14 virtual context
15
16 @r2 depends on patch@
17 expression E;
18 @@
19 - if (E != NULL)
20 (
21   kfree(E);
22 |
23   kzfree(E);
24 |
25   debugfs_remove(E);
26 |
27   debugfs_remove_recursive(E);
28 |
29   usb_free_urb(E);
30 |
31   kmem_cache_destroy(E);
32 |
33   mempool_destroy(E);
34 |
35   dma_pool_destroy(E);
36 )
37
38 @r depends on context || report || org @
39 expression E;
40 position p;
41 @@
42
43 * if (E != NULL)
44 *       \(kfree@p\|kzfree@p\|debugfs_remove@p\|debugfs_remove_recursive@p\|
45 *         usb_free_urb@p\|kmem_cache_destroy@p\|mempool_destroy@p\|
46 *         dma_pool_destroy@p\)(E);
47
48 @script:python depends on org@
49 p << r.p;
50 @@
51
52 cocci.print_main("NULL check before that freeing function is not needed", p)
53
54 @script:python depends on report@
55 p << r.p;
56 @@
57
58 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."
59 coccilib.report.print_report(p[0], msg)