| 1 |
/* { dg-additional-options "-Wno-analyzer-too-complex" } */ |
| 2 |
/* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */ |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
struct foo { int dummy; }; |
| 7 |
|
| 8 |
struct foo ** |
| 9 |
test (int n) { |
| 10 |
struct foo **arr; |
| 11 |
int i; |
| 12 |
|
| 13 |
if ((arr = (struct foo **)malloc(n * sizeof(struct foo *))) == NULL) |
| 14 |
return NULL; |
| 15 |
|
| 16 |
for (i = 0; i < n; i++) { |
| 17 |
if ((arr[i] = (struct foo *)malloc(sizeof(struct foo))) == NULL) { |
| 18 |
for (; i >= 0; i++) { /* { dg-warning "infinite loop" } */ |
| 19 |
/* This loop is in the wrong direction, so not technically an |
| 20 |
infinite loop ("i" will eventually wrap around), but the |
| 21 |
analyzer's condition handling treats the overflow as such. |
| 22 |
In any case, the code is suspect and warrants a warning. */ |
| 23 |
free(arr[i]); /* { dg-bogus "double-'free'" } */ |
| 24 |
} |
| 25 |
free(arr); /* { dg-warning "leak" } */ |
| 26 |
return NULL; |
| 27 |
} |
| 28 |
} |
| 29 |
return arr; |
| 30 |
} |