Thomas Gleixner | 7f904d7 | 2019-06-04 10:11:38 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 2 | /// 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 | /// |
Thomas Gleixner | 7f904d7 | 2019-06-04 10:11:38 +0200 | [diff] [blame] | 8 | // Copyright: (C) 2014 Fabian Frederick. |
Fabian Frederick | 45715f3 | 2014-08-08 14:23:31 -0700 | [diff] [blame] | 9 | // Comments: - |
| 10 | // Options: --no-includes --include-headers |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 11 | |
| 12 | virtual patch |
| 13 | virtual org |
| 14 | virtual report |
| 15 | virtual context |
| 16 | |
| 17 | @r2 depends on patch@ |
| 18 | expression E; |
| 19 | @@ |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 20 | - if (E != NULL) |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 21 | ( |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 22 | kfree(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 23 | | |
Denis Efremov | 5e0c074 | 2020-09-02 07:40:59 +0300 | [diff] [blame] | 24 | kvfree(E); |
| 25 | | |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 26 | kfree_sensitive(E); |
Yann Droneaud | 6dd9379 | 2016-05-23 17:07:19 +0200 | [diff] [blame] | 27 | | |
Denis Efremov | 5e0c074 | 2020-09-02 07:40:59 +0300 | [diff] [blame] | 28 | kvfree_sensitive(E, ...); |
| 29 | | |
| 30 | vfree(E); |
| 31 | | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 32 | debugfs_remove(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 33 | | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 34 | debugfs_remove_recursive(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 35 | | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 36 | usb_free_urb(E); |
Julia Lawall | 4743775 | 2015-10-26 22:28:26 +0100 | [diff] [blame] | 37 | | |
| 38 | kmem_cache_destroy(E); |
| 39 | | |
| 40 | mempool_destroy(E); |
| 41 | | |
| 42 | dma_pool_destroy(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | @r depends on context || report || org @ |
| 46 | expression E; |
| 47 | position p; |
| 48 | @@ |
| 49 | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 50 | * if (E != NULL) |
Denis Efremov | 5e0c074 | 2020-09-02 07:40:59 +0300 | [diff] [blame] | 51 | * \(kfree@p\|kvfree@p\|kfree_sensitive@p\|kvfree_sensitive@p\|vfree@p\| |
| 52 | * debugfs_remove@p\|debugfs_remove_recursive@p\| |
Julia Lawall | 4743775 | 2015-10-26 22:28:26 +0100 | [diff] [blame] | 53 | * usb_free_urb@p\|kmem_cache_destroy@p\|mempool_destroy@p\| |
Denis Efremov | 5e0c074 | 2020-09-02 07:40:59 +0300 | [diff] [blame] | 54 | * dma_pool_destroy@p\)(E, ...); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 55 | |
| 56 | @script:python depends on org@ |
| 57 | p << r.p; |
| 58 | @@ |
| 59 | |
| 60 | cocci.print_main("NULL check before that freeing function is not needed", p) |
| 61 | |
| 62 | @script:python depends on report@ |
| 63 | p << r.p; |
| 64 | @@ |
| 65 | |
Himanshu Jha | 3e47599 | 2018-01-13 21:28:15 +0530 | [diff] [blame] | 66 | msg = "WARNING: NULL check before some freeing functions is not needed." |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 67 | coccilib.report.print_report(p[0], msg) |