Heinrich Schuchardt | 084462d | 2020-04-19 11:07:34 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /// Remove casting the values returned by memory allocation functions |
| 3 | /// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc. |
| 4 | /// |
| 5 | //# This makes an effort to find cases of casting of values returned by |
| 6 | //# malloc, calloc, kmalloc, kmalloc_array, kmalloc_node and removes |
| 7 | //# the casting as it is not required. The result in the patch case may |
| 8 | //# need some reformatting. |
| 9 | // |
| 10 | // Confidence: High |
| 11 | // Copyright: (C) 2014 Himangi Saraogi |
| 12 | // Copyright: (C) 2017 Himanshu Jha |
| 13 | // Comments: |
| 14 | // Options: --no-includes --include-headers |
| 15 | // |
| 16 | |
| 17 | virtual context |
| 18 | virtual patch |
| 19 | virtual org |
| 20 | virtual report |
| 21 | |
| 22 | @initialize:python@ |
| 23 | @@ |
| 24 | import re |
| 25 | pattern = '__' |
| 26 | m = re.compile(pattern) |
| 27 | |
| 28 | @r1 depends on context || patch@ |
| 29 | type T; |
| 30 | @@ |
| 31 | |
| 32 | (T *) |
| 33 | \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...) |
| 34 | |
| 35 | //---------------------------------------------------------- |
| 36 | // For context mode |
| 37 | //---------------------------------------------------------- |
| 38 | |
| 39 | @script:python depends on context@ |
| 40 | t << r1.T; |
| 41 | @@ |
| 42 | |
| 43 | if m.search(t) != None: |
| 44 | cocci.include_match(False) |
| 45 | |
| 46 | @depends on context && r1@ |
| 47 | type r1.T; |
| 48 | @@ |
| 49 | |
| 50 | * (T *) |
| 51 | \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...) |
| 52 | |
| 53 | //---------------------------------------------------------- |
| 54 | // For patch mode |
| 55 | //---------------------------------------------------------- |
| 56 | |
| 57 | @script:python depends on patch@ |
| 58 | t << r1.T; |
| 59 | @@ |
| 60 | |
| 61 | if m.search(t) != None: |
| 62 | cocci.include_match(False) |
| 63 | |
| 64 | @depends on patch && r1@ |
| 65 | type r1.T; |
| 66 | @@ |
| 67 | |
| 68 | - (T *) |
| 69 | \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...) |
| 70 | |
| 71 | //---------------------------------------------------------- |
| 72 | // For org and report mode |
| 73 | //---------------------------------------------------------- |
| 74 | |
| 75 | @r2 depends on org || report@ |
| 76 | type T; |
| 77 | position p; |
| 78 | @@ |
| 79 | |
| 80 | (T@p *) |
| 81 | \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...) |
| 82 | |
| 83 | @script:python depends on org@ |
| 84 | p << r2.p; |
| 85 | t << r2.T; |
| 86 | @@ |
| 87 | |
| 88 | if m.search(t) != None: |
| 89 | cocci.include_match(False) |
| 90 | else: |
| 91 | coccilib.org.print_safe_todo(p[0], t) |
| 92 | |
| 93 | @script:python depends on report@ |
| 94 | p << r2.p; |
| 95 | t << r2.T; |
| 96 | @@ |
| 97 | |
| 98 | if m.search(t) != None: |
| 99 | cocci.include_match(False) |
| 100 | else: |
| 101 | msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t) |
| 102 | coccilib.report.print_report(p[0], msg) |