1 // RUN: %clang_analyze_cc1 -fblocks -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.Malloc
5 // RUN: %clang_analyze_cc1 -fblocks -verify %s \
6 // RUN: -analyzer-checker=core \
7 // RUN: -analyzer-checker=unix.Malloc \
8 // RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true
9 typedef __typeof(sizeof(int)) size_t;
16 // expected-warning@-1{{Argument to 'free()' is the address of the local variable 'a', which is not memory allocated by 'malloc()'}}
17 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
23 // expected-warning@-1{{Argument to 'free()' is the address of the local variable 'a', which is not memory allocated by 'malloc()'}}
24 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
28 static int a
[] = { 1 };
30 // expected-warning@-1{{Argument to 'free()' is the address of the static variable 'a', which is not memory allocated by 'malloc()'}}
31 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
35 free(x
); // no-warning
39 extern char *ptr(void);
40 free(ptr()); // no-warning
45 // expected-warning@-1{{Argument to 'free()' is a constant address (1000), which is not memory allocated by 'malloc()'}}
46 // expected-warning@-2{{attempt to call free on non-heap object '(void *)1000'}}
50 free(*x
); // no-warning
55 free((*x
)+8); // no-warning
61 // expected-warning@-1{{Argument to 'free()' is the address of the label 'label', which is not memory allocated by 'malloc()'}}
62 // expected-warning@-2{{attempt to call free on non-heap object 'label'}}
67 // expected-warning@-1{{Argument to 'free()' is the address of the function 't10', which is not memory allocated by 'malloc()'}}
68 // expected-warning@-2{{attempt to call free on non-heap object 't10'}}
72 char *p
= (char*)alloca(2);
73 free(p
); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}}
77 char *p
= (char*)__builtin_alloca(2);
78 free(p
); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}}
83 // expected-warning@-1{{Argument to 'free()' is a block, which is not memory allocated by 'malloc()'}}
84 // expected-warning@-2{{attempt to call free on non-heap object: block expression}}
89 // expected-warning@-1{{Argument to 'free()' is the address of the parameter 'a', which is not memory allocated by 'malloc()'}}
90 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
93 static int someGlobal
[2];
96 // expected-warning@-1{{Argument to 'free()' is the address of the global variable 'someGlobal', which is not memory allocated by 'malloc()'}}
97 // expected-warning@-2{{attempt to call free on non-heap object 'someGlobal'}}
100 void t16 (char **x
, int offset
) {
102 free(x
[offset
]); // no-warning
107 free(iptr
); // Oops, forgot to call iptr().
108 // expected-warning@-1{{Argument to 'free()' is the address of the function 'iptr', which is not memory allocated by 'malloc()'}}
109 // expected-warning@-2{{attempt to call free on non-heap object 'iptr'}}
116 void t18 (struct S s
) {
117 free((void*)(unsigned long long)s
.p
); // no warning