1 // SPDX-License-Identifier: GPL-2.0-only
3 /// Check for array_size(), array3_size(), struct_size() duplicates.
4 /// These patterns are detected:
5 /// 1. An opencoded expression is used before array_size() to compute the same size
6 /// 2. An opencoded expression is used after array_size() to compute the same size
7 /// From security point of view only first case is relevant. These functions
8 /// perform arithmetic overflow check. Thus, if we use an opencoded expression
9 /// before a call to the *_size() function we can miss an overflow.
12 // Copyright: (C) 2020 Denis Efremov ISPRAS
13 // Options: --no-includes --include-headers --no-loops
26 expression subE1 <= as.E1;
27 expression subE2 <= as.E2;
28 expression as.E1, as.E2, E3;
29 assignment operator aop;
34 ... when != \(subE1\|subE2\) aop E3
35 when != &\(subE1\|subE2\)
36 * array_size(E1, E2)@p2
38 @script:python depends on report@
43 msg = "WARNING: array_size is used later (line %s) to compute the same size" % (p2[0].line)
44 coccilib.report.print_report(p1[0], msg)
46 @script:python depends on org@
51 msg = "WARNING: array_size is used later (line %s) to compute the same size" % (p2[0].line)
52 coccilib.org.print_todo(p1[0], msg)
55 expression subE1 <= as.E1;
56 expression subE2 <= as.E2;
57 expression as.E1, as.E2, E3;
58 assignment operator aop;
62 * array_size(E1, E2)@p1
63 ... when != \(subE1\|subE2\) aop E3
64 when != &\(subE1\|subE2\)
67 @script:python depends on report@
72 msg = "WARNING: array_size is already used (line %s) to compute the same size" % (p1[0].line)
73 coccilib.report.print_report(p2[0], msg)
75 @script:python depends on org@
80 msg = "WARNING: array_size is already used (line %s) to compute the same size" % (p1[0].line)
81 coccilib.org.print_todo(p2[0], msg)
84 expression E1, E2, E3;
87 array3_size(E1, E2, E3)
90 expression subE1 <= as3.E1;
91 expression subE2 <= as3.E2;
92 expression subE3 <= as3.E3;
93 expression as3.E1, as3.E2, as3.E3, E4;
94 assignment operator aop;
99 ... when != \(subE1\|subE2\|subE3\) aop E4
100 when != &\(subE1\|subE2\|subE3\)
101 * array3_size(E1, E2, E3)@p2
103 @script:python depends on report@
108 msg = "WARNING: array3_size is used later (line %s) to compute the same size" % (p2[0].line)
109 coccilib.report.print_report(p1[0], msg)
111 @script:python depends on org@
116 msg = "WARNING: array3_size is used later (line %s) to compute the same size" % (p2[0].line)
117 coccilib.org.print_todo(p1[0], msg)
120 expression subE1 <= as3.E1;
121 expression subE2 <= as3.E2;
122 expression subE3 <= as3.E3;
123 expression as3.E1, as3.E2, as3.E3, E4;
124 assignment operator aop;
128 * array3_size(E1, E2, E3)@p1
129 ... when != \(subE1\|subE2\|subE3\) aop E4
130 when != &\(subE1\|subE2\|subE3\)
133 @script:python depends on report@
138 msg = "WARNING: array3_size is already used (line %s) to compute the same size" % (p1[0].line)
139 coccilib.report.print_report(p2[0], msg)
141 @script:python depends on org@
146 msg = "WARNING: array3_size is already used (line %s) to compute the same size" % (p1[0].line)
147 coccilib.org.print_todo(p2[0], msg)
150 expression E1, E2, E3;
153 struct_size(E1, E2, E3)
156 expression subE3 <= ss.E3;
157 expression ss.E1, ss.E2, ss.E3, E4;
158 assignment operator aop;
163 ... when != subE3 aop E4
165 * struct_size(E1, E2, E3)@p2
167 @script:python depends on report@
172 msg = "WARNING: struct_size is used later (line %s) to compute the same size" % (p2[0].line)
173 coccilib.report.print_report(p1[0], msg)
175 @script:python depends on org@
180 msg = "WARNING: struct_size is used later (line %s) to compute the same size" % (p2[0].line)
181 coccilib.org.print_todo(p1[0], msg)
184 expression subE3 <= ss.E3;
185 expression ss.E1, ss.E2, ss.E3, E4;
186 assignment operator aop;
190 * struct_size(E1, E2, E3)@p1
191 ... when != subE3 aop E4
195 @script:python depends on report@
200 msg = "WARNING: struct_size is already used (line %s) to compute the same size" % (p1[0].line)
201 coccilib.report.print_report(p2[0], msg)
203 @script:python depends on org@
208 msg = "WARNING: struct_size is already used (line %s) to compute the same size" % (p1[0].line)
209 coccilib.org.print_todo(p2[0], msg)