aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/analyzer/infinite-loop-4.c
blob: 032f7fd586026a3410488ad16c0b4d0b5ecc238e (plain)
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
/* Various tests for loops that aren't infinite, strictly speaking,
   but look bogus.  */

// TODO: should we complain about these?

extern int maybe_useful_work ();

/* Loop iteration going the wrong way, with a signed iterator

   Not infinite, as will eventually overflow and bail out, but probably
   not what the user intended.  */

void test_wrong_way_signed_1 (int n)
{
  for (int i = 0; i < n; i--)
    {
    }
}

void test_wrong_way_signed_2 (int n)
{
  for (int i = 0; i < n; i--)
    maybe_useful_work ();
}

int test_wrong_way_signed_3 (int *arr, int n)
{
  int sum = 0;
  for (int i = 0; i < n; i--)
    sum += arr[i];
  return sum;
}


/* As above, but with an unsigned iterator.

   Not infinite, as will immediately overflow and bail out, but probably
   not what the user intended.  */

void test_wrong_way_unsigned_1 (unsigned n)
{
  for (unsigned i = 0; i < n; i--)
    {
    }
}

void test_wrong_way_unsigned_2 (unsigned n)
{
  for (unsigned i = 0; i < n; i--)
    maybe_useful_work ();
}

int test_wrong_way_unsigned_3 (int *arr, unsigned n)
{
  int sum = 0;
  for (unsigned i = 0; i < n; i--)
    sum += arr[i];
  return sum;
}

/* BUG: "n" never changes, so loop is never entered.  */

void test_1 (void)
{
  int n = 0;

  /* [...snip...]  */

  for (int i = 0; i < n; i++)
    maybe_useful_work ();
}