blob: 2d59545d7d454d0cf51f20113049c6cc1c6ec95f [file] [log] [blame]
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +02001// SPDX-License-Identifier: GPL-2.0-only
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +01002/// NULL check before some freeing functions is not needed.
3///
4/// Based on checkpatch warning
5/// "kfree(NULL) is safe this check is probably not required"
6/// and kfreeaddr.cocci by Julia Lawall.
7///
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +02008// Copyright: (C) 2014 Fabian Frederick.
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +01009// Comments: -
10// Options: --no-includes --include-headers
11
12virtual patch
13virtual org
14virtual report
15virtual context
16
17@r2 depends on patch@
18expression E;
19@@
20- if (E != NULL)
21(
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020022 free(E);
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010023|
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020024 kfree(E);
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010025|
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020026 vfree(E);
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010027|
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020028 vfree_recursive(E);
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010029|
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020030 kmem_cache_free(E);
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010031|
32 kmem_cache_destroy(E);
33|
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020034 gzfree(E);
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010035)
36
37@r depends on context || report || org @
38expression E;
39position p;
40@@
41
42* if (E != NULL)
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020043* \(free@p\|kfree@p\|vfree@p\|debugfs_remove_recursive@p\|
44* kmem_cache_free@p\|kmem_cache_destroy@p\|gzfree@p\)(E);
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010045
46@script:python depends on org@
47p << r.p;
48@@
49
50cocci.print_main("NULL check before that freeing function is not needed", p)
51
52@script:python depends on report@
53p << r.p;
54@@
55
Heinrich Schuchardt8fb98422020-04-19 11:56:02 +020056msg = "WARNING: NULL check before some freeing functions is not needed."
Heinrich Schuchardtb38d6ca2017-11-10 19:15:02 +010057coccilib.report.print_report(p[0], msg)