1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-checker=core,unix.Malloc -fblocks -verify %s
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-checker=core,alpha.unix.MallocWithAnnotations -fblocks -verify %s
3*f4a2713aSLionel Sambuc void free(void *);
4*f4a2713aSLionel Sambuc 
t1()5*f4a2713aSLionel Sambuc void t1 () {
6*f4a2713aSLionel Sambuc   int a[] = { 1 };
7*f4a2713aSLionel Sambuc   free(a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
8*f4a2713aSLionel Sambuc }
9*f4a2713aSLionel Sambuc 
t2()10*f4a2713aSLionel Sambuc void t2 () {
11*f4a2713aSLionel Sambuc   int a = 1;
12*f4a2713aSLionel Sambuc   free(&a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
13*f4a2713aSLionel Sambuc }
14*f4a2713aSLionel Sambuc 
t3()15*f4a2713aSLionel Sambuc void t3 () {
16*f4a2713aSLionel Sambuc   static int a[] = { 1 };
17*f4a2713aSLionel Sambuc   free(a); // expected-warning {{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
18*f4a2713aSLionel Sambuc }
19*f4a2713aSLionel Sambuc 
t4(char * x)20*f4a2713aSLionel Sambuc void t4 (char *x) {
21*f4a2713aSLionel Sambuc   free(x); // no-warning
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc 
t5()24*f4a2713aSLionel Sambuc void t5 () {
25*f4a2713aSLionel Sambuc   extern char *ptr();
26*f4a2713aSLionel Sambuc   free(ptr()); // no-warning
27*f4a2713aSLionel Sambuc }
28*f4a2713aSLionel Sambuc 
t6()29*f4a2713aSLionel Sambuc void t6 () {
30*f4a2713aSLionel Sambuc   free((void*)1000); // expected-warning {{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
31*f4a2713aSLionel Sambuc }
32*f4a2713aSLionel Sambuc 
t7(char ** x)33*f4a2713aSLionel Sambuc void t7 (char **x) {
34*f4a2713aSLionel Sambuc   free(*x); // no-warning
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
t8(char ** x)37*f4a2713aSLionel Sambuc void t8 (char **x) {
38*f4a2713aSLionel Sambuc   // ugh
39*f4a2713aSLionel Sambuc   free((*x)+8); // no-warning
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
t9()42*f4a2713aSLionel Sambuc void t9 () {
43*f4a2713aSLionel Sambuc label:
44*f4a2713aSLionel Sambuc   free(&&label); // expected-warning {{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
45*f4a2713aSLionel Sambuc }
46*f4a2713aSLionel Sambuc 
t10()47*f4a2713aSLionel Sambuc void t10 () {
48*f4a2713aSLionel Sambuc   free((void*)&t10); // expected-warning {{Argument to free() is the address of the function 't10', which is not memory allocated by malloc()}}
49*f4a2713aSLionel Sambuc }
50*f4a2713aSLionel Sambuc 
t11()51*f4a2713aSLionel Sambuc void t11 () {
52*f4a2713aSLionel Sambuc   char *p = (char*)__builtin_alloca(2);
53*f4a2713aSLionel Sambuc   free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
t12()56*f4a2713aSLionel Sambuc void t12 () {
57*f4a2713aSLionel Sambuc   free(^{return;}); // expected-warning {{Argument to free() is a block, which is not memory allocated by malloc()}}
58*f4a2713aSLionel Sambuc }
59*f4a2713aSLionel Sambuc 
t13(char a)60*f4a2713aSLionel Sambuc void t13 (char a) {
61*f4a2713aSLionel Sambuc   free(&a); // expected-warning {{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
62*f4a2713aSLionel Sambuc }
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc static int someGlobal[2];
t14()65*f4a2713aSLionel Sambuc void t14 () {
66*f4a2713aSLionel Sambuc   free(someGlobal); // expected-warning {{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
67*f4a2713aSLionel Sambuc }
68*f4a2713aSLionel Sambuc 
t15(char ** x,int offset)69*f4a2713aSLionel Sambuc void t15 (char **x, int offset) {
70*f4a2713aSLionel Sambuc   // Unknown value
71*f4a2713aSLionel Sambuc   free(x[offset]); // no-warning
72*f4a2713aSLionel Sambuc }
73