bugprone-infinite-loop.cpp
7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// RUN: %check_clang_tidy %s bugprone-infinite-loop %t -- -- -fexceptions
void simple_infinite_loop1() {
int i = 0;
int j = 0;
while (i < 10) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
j++;
}
while (int k = 10) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; it does not check any variables in the condition [bugprone-infinite-loop]
j--;
}
while (int k = 10) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; it does not check any variables in the condition [bugprone-infinite-loop]
k--;
}
do {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
j++;
} while (i < 10);
for (i = 0; i < 10; ++j) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
}
}
void simple_infinite_loop2() {
int i = 0;
int j = 0;
int Limit = 10;
while (i < Limit) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i, Limit) are updated in the loop body [bugprone-infinite-loop]
j++;
}
while (int k = Limit) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (Limit) are updated in the loop body [bugprone-infinite-loop]
j--;
}
while (int k = Limit) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (Limit) are updated in the loop body [bugprone-infinite-loop]
k--;
}
do {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i, Limit) are updated in the loop body [bugprone-infinite-loop]
j++;
} while (i < Limit);
for (i = 0; i < Limit; ++j) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i, Limit) are updated in the loop body [bugprone-infinite-loop]
}
}
void simple_not_infinite1() {
int i = 0;
int Limit = 100;
while (i < Limit) {
// Not an error since 'Limit' is updated.
Limit--;
}
while (Limit--) {
// Not an error since 'Limit' is updated.
i++;
}
while ((Limit)--) {
// Not an error since 'Limit' is updated.
i++;
}
while ((Limit) -= 1) {
// Not an error since 'Limit' is updated.
}
while (int k = Limit) {
// Not an error since 'Limit' is updated.
Limit--;
}
while (int k = Limit) {
// Not an error since 'Limit' is updated
(Limit)--;
}
while (int k = Limit--) {
// Not an error since 'Limit' is updated.
i++;
}
do {
Limit--;
} while (i < Limit);
for (i = 0; i < Limit; Limit--) {
}
for (i = 0; i < Limit; (Limit) = Limit - 1) {
}
for (i = 0; i < Limit; (Limit) -= 1) {
}
for (i = 0; i < Limit; --(Limit)) {
}
}
void simple_not_infinite2() {
for (int i = 10; i-- > 0;) {
// Not an error, since loop variable is modified in its condition part.
}
}
int unknown_function();
void function_call() {
int i = 0;
while (i < unknown_function()) {
// Not an error, since the function may return different values.
}
do {
// Not an error, since the function may return different values.
} while (i < unknown_function());
for (i = 0; i < unknown_function();) {
// Not an error, since the function may return different values.
}
}
void escape_before1() {
int i = 0;
int Limit = 100;
int *p = &i;
while (i < Limit) {
// Not an error, since *p is alias of i.
(*p)++;
}
do {
(*p)++;
} while (i < Limit);
for (i = 0; i < Limit; ++(*p)) {
}
}
void escape_before2() {
int i = 0;
int Limit = 100;
int &ii = i;
while (i < Limit) {
// Not an error, since ii is alias of i.
ii++;
}
do {
ii++;
} while (i < Limit);
for (i = 0; i < Limit; ++ii) {
}
}
void escape_inside1() {
int i = 0;
int Limit = 100;
int *p = &i;
while (i < Limit) {
// Not an error, since *p is alias of i.
int *p = &i;
(*p)++;
}
do {
int *p = &i;
(*p)++;
} while (i < Limit);
}
void escape_inside2() {
int i = 0;
int Limit = 100;
while (i < Limit) {
// Not an error, since ii is alias of i.
int &ii = i;
ii++;
}
do {
int &ii = i;
ii++;
} while (i < Limit);
}
void escape_after1() {
int i = 0;
int j = 0;
int Limit = 10;
while (i < Limit) {
// False negative, but difficult to detect without CFG-based analysis
}
int *p = &i;
}
void escape_after2() {
int i = 0;
int j = 0;
int Limit = 10;
while (i < Limit) {
// False negative, but difficult to detect without CFG-based analysis
}
int &ii = i;
}
int glob;
void global1(int &x) {
int i = 0, Limit = 100;
while (x < Limit) {
// Not an error since 'x' can be an alias of 'glob'.
glob++;
}
}
void global2() {
int i = 0, Limit = 100;
while (glob < Limit) {
// Since 'glob' is declared out of the function we do not warn.
i++;
}
}
struct X {
int m;
void change_m();
void member_expr1(int i) {
while (i < m) {
// False negative: No warning, since skipping the case where a struct or
// class can be found in its condition.
;
}
}
void member_expr2(int i) {
while (i < m) {
--m;
}
}
void member_expr3(int i) {
while (i < m) {
change_m();
}
}
};
void array_index() {
int i = 0;
int v[10];
while (i < 10) {
v[i++] = 0;
}
i = 0;
do {
v[i++] = 0;
} while (i < 9);
for (i = 0; i < 10;) {
v[i++] = 0;
}
for (i = 0; i < 10; v[i++] = 0) {
}
}
void no_loop_variable() {
while (0)
;
}
void volatile_in_condition() {
volatile int cond = 0;
while (!cond) {
}
}
namespace std {
template<typename T> class atomic {
T val;
public:
atomic(T v): val(v) {};
operator T() { return val; };
};
}
void atomic_in_condition() {
std::atomic<int> cond = 0;
while (!cond) {
}
}
void loop_exit1() {
int i = 0;
while (i) {
if (unknown_function())
break;
}
}
void loop_exit2() {
int i = 0;
while (i) {
if (unknown_function())
return;
}
}
void loop_exit3() {
int i = 0;
while (i) {
if (unknown_function())
goto end;
}
end:
;
}
void loop_exit4() {
int i = 0;
while (i) {
if (unknown_function())
throw 1;
}
}
[[noreturn]] void exit(int);
void loop_exit5() {
int i = 0;
while (i) {
if (unknown_function())
exit(1);
}
}
void loop_exit_in_lambda() {
int i = 0;
while (i) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
auto l = []() { return 0; };
}
}
void lambda_capture() {
int i = 0;
int Limit = 100;
int *p = &i;
while (i < Limit) {
// Not an error, since i is captured by reference in a lambda.
auto l = [&i]() { ++i; };
}
do {
int *p = &i;
(*p)++;
} while (i < Limit);
}
void evaluatable(bool CondVar) {
for (; false && CondVar;) {
}
while (false && CondVar) {
}
do {
} while (false && CondVar);
}