| 1 |
// RUN: %clang_cc1 -Wfree-nonheap-object -fsyntax-only -verify %s |
| 2 |
|
| 3 |
typedef __SIZE_TYPE__ size_t; |
| 4 |
void *malloc(size_t); |
| 5 |
void free(void *); |
| 6 |
|
| 7 |
struct S { |
| 8 |
int I; |
| 9 |
char *P; |
| 10 |
}; |
| 11 |
|
| 12 |
int GI; |
| 13 |
void test(void) { |
| 14 |
{ |
| 15 |
free(&GI); // expected-warning {{attempt to call free on non-heap object 'GI'}} |
| 16 |
} |
| 17 |
{ |
| 18 |
static int SI = 0; |
| 19 |
free(&SI); // expected-warning {{attempt to call free on non-heap object 'SI'}} |
| 20 |
} |
| 21 |
{ |
| 22 |
int I = 0; |
| 23 |
free(&I); // expected-warning {{attempt to call free on non-heap object 'I'}} |
| 24 |
} |
| 25 |
{ |
| 26 |
int I = 0; |
| 27 |
int *P = &I; |
| 28 |
free(P); // FIXME diagnosing this would require control flow analysis. |
| 29 |
} |
| 30 |
{ |
| 31 |
void *P = malloc(8); |
| 32 |
free(P); |
| 33 |
} |
| 34 |
{ |
| 35 |
int A[] = {0, 1, 2, 3}; |
| 36 |
free(A); // expected-warning {{attempt to call free on non-heap object 'A'}} |
| 37 |
free(&A); // expected-warning {{attempt to call free on non-heap object 'A'}} |
| 38 |
} |
| 39 |
{ |
| 40 |
struct S s; |
| 41 |
free(&s.I); // expected-warning {{attempt to call free on non-heap object 'I'}} |
| 42 |
free(s.P); |
| 43 |
} |
| 44 |
} |