~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/coccinelle/misc/array_size_dup.cocci

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 ///
  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.
 10 ///
 11 // Confidence: High
 12 // Copyright: (C) 2020 Denis Efremov ISPRAS
 13 // Options: --no-includes --include-headers --no-loops
 14 
 15 virtual context
 16 virtual report
 17 virtual org
 18 
 19 @as@
 20 expression E1, E2;
 21 @@
 22 
 23 array_size(E1, E2)
 24 
 25 @as_next@
 26 expression subE1 <= as.E1;
 27 expression subE2 <= as.E2;
 28 expression as.E1, as.E2, E3;
 29 assignment operator aop;
 30 position p1, p2;
 31 @@
 32 
 33 * E1 * E2@p1
 34   ... when != \(subE1\|subE2\) aop E3
 35       when != &\(subE1\|subE2\)
 36 * array_size(E1, E2)@p2
 37 
 38 @script:python depends on report@
 39 p1 << as_next.p1;
 40 p2 << as_next.p2;
 41 @@
 42 
 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)
 45 
 46 @script:python depends on org@
 47 p1 << as_next.p1;
 48 p2 << as_next.p2;
 49 @@
 50 
 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)
 53 
 54 @as_prev@
 55 expression subE1 <= as.E1;
 56 expression subE2 <= as.E2;
 57 expression as.E1, as.E2, E3;
 58 assignment operator aop;
 59 position p1, p2;
 60 @@
 61 
 62 * array_size(E1, E2)@p1
 63   ... when != \(subE1\|subE2\) aop E3
 64       when != &\(subE1\|subE2\)
 65 * E1 * E2@p2
 66 
 67 @script:python depends on report@
 68 p1 << as_prev.p1;
 69 p2 << as_prev.p2;
 70 @@
 71 
 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)
 74 
 75 @script:python depends on org@
 76 p1 << as_prev.p1;
 77 p2 << as_prev.p2;
 78 @@
 79 
 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)
 82 
 83 @as3@
 84 expression E1, E2, E3;
 85 @@
 86 
 87 array3_size(E1, E2, E3)
 88 
 89 @as3_next@
 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;
 95 position p1, p2;
 96 @@
 97 
 98 * E1 * E2 * E3@p1
 99   ... when != \(subE1\|subE2\|subE3\) aop E4
100       when != &\(subE1\|subE2\|subE3\)
101 * array3_size(E1, E2, E3)@p2
102 
103 @script:python depends on report@
104 p1 << as3_next.p1;
105 p2 << as3_next.p2;
106 @@
107 
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)
110 
111 @script:python depends on org@
112 p1 << as3_next.p1;
113 p2 << as3_next.p2;
114 @@
115 
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)
118 
119 @as3_prev@
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;
125 position p1, p2;
126 @@
127 
128 * array3_size(E1, E2, E3)@p1
129   ... when != \(subE1\|subE2\|subE3\) aop E4
130       when != &\(subE1\|subE2\|subE3\)
131 * E1 * E2 * E3@p2
132 
133 @script:python depends on report@
134 p1 << as3_prev.p1;
135 p2 << as3_prev.p2;
136 @@
137 
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)
140 
141 @script:python depends on org@
142 p1 << as3_prev.p1;
143 p2 << as3_prev.p2;
144 @@
145 
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)
148 
149 @ss@
150 expression E1, E2, E3;
151 @@
152 
153 struct_size(E1, E2, E3)
154 
155 @ss_next@
156 expression subE3 <= ss.E3;
157 expression ss.E1, ss.E2, ss.E3, E4;
158 assignment operator aop;
159 position p1, p2;
160 @@
161 
162 * E1 * E2 + E3@p1
163   ... when != subE3 aop E4
164       when != &subE3
165 * struct_size(E1, E2, E3)@p2
166 
167 @script:python depends on report@
168 p1 << ss_next.p1;
169 p2 << ss_next.p2;
170 @@
171 
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)
174 
175 @script:python depends on org@
176 p1 << ss_next.p1;
177 p2 << ss_next.p2;
178 @@
179 
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)
182 
183 @ss_prev@
184 expression subE3 <= ss.E3;
185 expression ss.E1, ss.E2, ss.E3, E4;
186 assignment operator aop;
187 position p1, p2;
188 @@
189 
190 * struct_size(E1, E2, E3)@p1
191   ... when != subE3 aop E4
192       when != &subE3
193 * E1 * E2 + E3@p2
194 
195 @script:python depends on report@
196 p1 << ss_prev.p1;
197 p2 << ss_prev.p2;
198 @@
199 
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)
202 
203 @script:python depends on org@
204 p1 << ss_prev.p1;
205 p2 << ss_prev.p2;
206 @@
207 
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)

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php