blob: e336784a9fc97029208891508d7d79a3b545f96f [file] [log] [blame]
Heinrich Schuchardt084462d2020-04-19 11:07:34 +02001// 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
17virtual context
18virtual patch
19virtual org
20virtual report
21
22@initialize:python@
23@@
24import re
25pattern = '__'
26m = re.compile(pattern)
27
28@r1 depends on context || patch@
29type 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@
40t << r1.T;
41@@
42
43if m.search(t) != None:
44 cocci.include_match(False)
45
46@depends on context && r1@
47type 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@
58t << r1.T;
59@@
60
61if m.search(t) != None:
62 cocci.include_match(False)
63
64@depends on patch && r1@
65type 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@
76type T;
77position p;
78@@
79
80 (T@p *)
81 \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...)
82
83@script:python depends on org@
84p << r2.p;
85t << r2.T;
86@@
87
88if m.search(t) != None:
89 cocci.include_match(False)
90else:
91 coccilib.org.print_safe_todo(p[0], t)
92
93@script:python depends on report@
94p << r2.p;
95t << r2.T;
96@@
97
98if m.search(t) != None:
99 cocci.include_match(False)
100else:
101 msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
102 coccilib.report.print_report(p[0], msg)