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