Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 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 | /// |
Fabian Frederick | 45715f3 | 2014-08-08 14:23:31 -0700 | [diff] [blame] | 7 | // Copyright: (C) 2014 Fabian Frederick. GPLv2. |
| 8 | // Comments: - |
| 9 | // Options: --no-includes --include-headers |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 10 | |
| 11 | virtual patch |
| 12 | virtual org |
| 13 | virtual report |
| 14 | virtual context |
| 15 | |
| 16 | @r2 depends on patch@ |
| 17 | expression E; |
| 18 | @@ |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 19 | - if (E != NULL) |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 20 | ( |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 21 | kfree(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 22 | | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 23 | debugfs_remove(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 24 | | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 25 | debugfs_remove_recursive(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 26 | | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 27 | usb_free_urb(E); |
Julia Lawall | 4743775 | 2015-10-26 22:28:26 +0100 | [diff] [blame] | 28 | | |
| 29 | kmem_cache_destroy(E); |
| 30 | | |
| 31 | mempool_destroy(E); |
| 32 | | |
| 33 | dma_pool_destroy(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | @r depends on context || report || org @ |
| 37 | expression E; |
| 38 | position p; |
| 39 | @@ |
| 40 | |
Julia Lawall | ca047e7 | 2015-10-26 22:28:25 +0100 | [diff] [blame] | 41 | * if (E != NULL) |
Julia Lawall | 4743775 | 2015-10-26 22:28:26 +0100 | [diff] [blame] | 42 | * \(kfree@p\|debugfs_remove@p\|debugfs_remove_recursive@p\| |
| 43 | * usb_free_urb@p\|kmem_cache_destroy@p\|mempool_destroy@p\| |
| 44 | * dma_pool_destroy@p\)(E); |
Fabian Frederick | 791dfeb | 2014-08-08 14:23:29 -0700 | [diff] [blame] | 45 | |
| 46 | @script:python depends on org@ |
| 47 | p << r.p; |
| 48 | @@ |
| 49 | |
| 50 | cocci.print_main("NULL check before that freeing function is not needed", p) |
| 51 | |
| 52 | @script:python depends on report@ |
| 53 | p << r.p; |
| 54 | @@ |
| 55 | |
| 56 | 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." |
| 57 | coccilib.report.print_report(p[0], msg) |