Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/infinite-loop-4.c

raw
1 /* Various tests for loops that aren't infinite, strictly speaking,
2 but look bogus. */
3
4 // TODO: should we complain about these?
5
6 extern int maybe_useful_work ();
7
8 /* Loop iteration going the wrong way, with a signed iterator
9
10 Not infinite, as will eventually overflow and bail out, but probably
11 not what the user intended. */
12
13 void test_wrong_way_signed_1 (int n)
14 {
15 for (int i = 0; i < n; i--)
16 {
17 }
18 }
19
20 void test_wrong_way_signed_2 (int n)
21 {
22 for (int i = 0; i < n; i--)
23 maybe_useful_work ();
24 }
25
26 int test_wrong_way_signed_3 (int *arr, int n)
27 {
28 int sum = 0;
29 for (int i = 0; i < n; i--)
30 sum += arr[i];
31 return sum;
32 }
33
34
35 /* As above, but with an unsigned iterator.
36
37 Not infinite, as will immediately overflow and bail out, but probably
38 not what the user intended. */
39
40 void test_wrong_way_unsigned_1 (unsigned n)
41 {
42 for (unsigned i = 0; i < n; i--)
43 {
44 }
45 }
46
47 void test_wrong_way_unsigned_2 (unsigned n)
48 {
49 for (unsigned i = 0; i < n; i--)
50 maybe_useful_work ();
51 }
52
53 int test_wrong_way_unsigned_3 (int *arr, unsigned n)
54 {
55 int sum = 0;
56 for (unsigned i = 0; i < n; i--)
57 sum += arr[i];
58 return sum;
59 }
60
61 /* BUG: "n" never changes, so loop is never entered. */
62
63 void test_1 (void)
64 {
65 int n = 0;
66
67 /* [...snip...] */
68
69 for (int i = 0; i < n; i++)
70 maybe_useful_work ();
71 }