aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/infinite-loop-1.c
blob: c3515bb140a1dd1ca5587f566741ad54307eb4a6 (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
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
/* { dg-additional-options "-O0" } */

extern int maybe_useful_work ();

int global_var;
volatile int volatile_global_var;

struct st
{
  int x;
  int y;
};

static void __attribute__((noinline))
do_nothing (void)
{
}

void test_empty_while_true ()
{
  while (1) {} /* { dg-warning "infinite loop" } */
  /* { dg-message "looping back\.\.\." "" { target *-*-* } .-1 } */
  /* { dg-message "\.\.\.to here" "" { target *-*-* } .-2 } */
}

void test_empty_do_while ()
{
  do {} while (1);  /* { dg-warning "infinite loop" } */
  /* { dg-message "looping back\.\.\." "" { target *-*-* } .-1 } */
  /* { dg-message "\.\.\.to here" "" { target *-*-* } .-2 } */
}

void test_empty_for ()
{
  for (;;) {} /* { dg-warning "infinite loop" } */
  /* { dg-message "looping back\.\.\." "" { target *-*-* } .-1 } */
  /* { dg-message "\.\.\.to here" "" { target *-*-* } .-2 } */
}

void test_while_true_maybe_useful_work ()
{
  while (1)
    maybe_useful_work ();
}

void test_while_true_interproc_empty ()
{
  /* Depending on optimization level, location is sometimes
     on "while", sometimes on "do_nothing", and sometimes the
     unknown location.  */
  while (1)
    do_nothing ();  /* { dg-warning "infinite loop" } */
}

void test_while_true_increment_local ()
{
  int i = 0;
  while (1)
    i++;  /* { dg-warning "infinite loop" } */
}

void test_while_true_increment_global ()
{
  while (1)
    global_var++;
}

void test_guarded_while_true_increment_local (int flag)
{
  if (flag)
    {
      int i = 0;
      while (1)
	i++;  /* { dg-warning "infinite loop" } */
    }
}

void test_while_local_flag_increment_local (int flag)
{
  int i = 0;
  while (flag) /* { dg-warning "infinite loop" } */
    i++;
}

extern int check_flag (void);

void test_while_calling_fn (void)
{
  while (check_flag ())
    do_nothing ();
}

void test_missing_parens_on_call (void)
{
  while (check_flag)
    do_nothing ();  /* { dg-warning "infinite loop" } */
}

void test_iteration_copy_and_paste_error (int m, int n)
{
  /* Wrong variable is incremented in inner "for" loop, thus
     effectively an infinite loop.  */
  float arr[m][n];
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; i++) /* { dg-warning "infinite loop" } */
      arr[i][j] = 0.f;
}

void test_missing_comparison_in_for_condition_1 (int n)
{
  /* Should have been "i < n", rather than just "n".  */
  for (int i = 0; n; i++) /* { dg-warning "infinite loop" } */
    {
    }
}

int test_missing_comparison_in_for_condition_2 (int val, int *arr, int n)
{
  /* Should have been "i < n", rather than just "n".  */
  int acc = 0;
  for (int i = 0; n; i++) /* { dg-warning "infinite loop" } */
    acc += arr[i];
  return acc;
}

void test_non_volatile_local_1 (void)
{
  int flag = 0;
  while (!flag) /* { dg-warning "infinite loop" } */
    {
    }
}

void test_non_volatile_local_2a (void)
{
  int flag = 0;

  /* Perhaps should complain about this.
     Although the infinite loop might be doing useful work,
     "while (!flag)" is a misleading way to spell "infinite loop".  */
  while (!flag)
    maybe_useful_work ();
}

void test_non_volatile_local_2b (void)
{
  int flag = 0;

  while (!flag)
    flag = maybe_useful_work ();
}

void test_non_volatile_local_3a (int n)
{
  int i = 0;

  /* Perhaps should complain about this.
     Although the infinite loop might be doing useful work,
     "while (i < n)" is a misleading way to spell "infinite loop".  */
  while (i < n)
    maybe_useful_work ();
}

void test_non_volatile_local_3b (int n)
{
  int i = 0;

  while (i < n)
    {
      maybe_useful_work ();
      i++;
    }
}

void test_volatile_local (void)
{
  volatile int flag = 0;
  while (!flag) /* { dg-bogus "infinite loop" } */
    {
    }
}

void test_non_volatile_global (void)
{
  /* Not sure if we should warn here.  */
  while (!global_var) /* { dg-warning "infinite loop" } */
    {
    }
}

void test_volatile_global (void)
{
  while (!volatile_global_var) /* { dg-bogus "infinite loop" } */
    {
    }
}

void test_field_1 (struct st *p)
{
  /* Not sure if we should warn here.  */
  while (!p->x) /* { dg-warning "infinite loop" } */
    {
    }
}

void test_field_2 (struct st *p)
{
  while (!p->x) /* { dg-bogus "infinite loop" } */
    maybe_useful_work ();
}


int missing_init_of_i (int *arr, unsigned n)
{
  int sum = 0;
  for (int i; i < n; i--) /* { dg-warning "use of uninitialized value 'i'" } */
    sum += arr[i];
  return sum;
}

void test_switch (char *pc)
{
  while (1)
    {
      char opcode = *pc; /* { dg-warning "infinite loop" } */
      switch (opcode) /* { dg-message "if it ever follows 'default:' branch, it will always do so\.\.\." } */
	{
	case 'A':
	  pc++;
	  break;
	case 'B':
	  return;
	}
    }
}