1*ffe7950eSBalazs Benics // RUN: %clang_analyze_cc1 -fblocks -verify %s \
24f395db8SChristopher Di Bella // RUN: -analyzer-checker=core \
34f395db8SChristopher Di Bella // RUN: -analyzer-checker=unix.Malloc
44f395db8SChristopher Di Bella //
5*ffe7950eSBalazs Benics // RUN: %clang_analyze_cc1 -fblocks -verify %s \
64f395db8SChristopher Di Bella // RUN: -analyzer-checker=core \
74f395db8SChristopher Di Bella // RUN: -analyzer-checker=unix.Malloc \
84f395db8SChristopher Di Bella // RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true
94f395db8SChristopher Di Bella namespace std {
104f395db8SChristopher Di Bella using size_t = decltype(sizeof(int));
114f395db8SChristopher Di Bella void free(void *);
124f395db8SChristopher Di Bella }
134f395db8SChristopher Di Bella
144f395db8SChristopher Di Bella extern "C" void free(void *);
154f395db8SChristopher Di Bella extern "C" void *alloca(std::size_t);
164f395db8SChristopher Di Bella
t1a()174f395db8SChristopher Di Bella void t1a () {
184f395db8SChristopher Di Bella int a[] = { 1 };
194f395db8SChristopher Di Bella free(a);
204f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
214f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
224f395db8SChristopher Di Bella }
234f395db8SChristopher Di Bella
t1b()244f395db8SChristopher Di Bella void t1b () {
254f395db8SChristopher Di Bella int a[] = { 1 };
264f395db8SChristopher Di Bella std::free(a);
274f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
284f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
294f395db8SChristopher Di Bella }
304f395db8SChristopher Di Bella
t2a()314f395db8SChristopher Di Bella void t2a () {
324f395db8SChristopher Di Bella int a = 1;
334f395db8SChristopher Di Bella free(&a);
344f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
354f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
364f395db8SChristopher Di Bella }
374f395db8SChristopher Di Bella
t2b()384f395db8SChristopher Di Bella void t2b () {
394f395db8SChristopher Di Bella int a = 1;
404f395db8SChristopher Di Bella std::free(&a);
414f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
424f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
434f395db8SChristopher Di Bella }
444f395db8SChristopher Di Bella
t3a()454f395db8SChristopher Di Bella void t3a () {
464f395db8SChristopher Di Bella static int a[] = { 1 };
474f395db8SChristopher Di Bella free(a);
484f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
494f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
504f395db8SChristopher Di Bella }
514f395db8SChristopher Di Bella
t3b()524f395db8SChristopher Di Bella void t3b () {
534f395db8SChristopher Di Bella static int a[] = { 1 };
544f395db8SChristopher Di Bella std::free(a);
554f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
564f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
574f395db8SChristopher Di Bella }
584f395db8SChristopher Di Bella
t4a(char * x)594f395db8SChristopher Di Bella void t4a (char *x) {
604f395db8SChristopher Di Bella free(x); // no-warning
614f395db8SChristopher Di Bella }
624f395db8SChristopher Di Bella
t4b(char * x)634f395db8SChristopher Di Bella void t4b (char *x) {
644f395db8SChristopher Di Bella std::free(x); // no-warning
654f395db8SChristopher Di Bella }
664f395db8SChristopher Di Bella
t5a()674f395db8SChristopher Di Bella void t5a () {
684f395db8SChristopher Di Bella extern char *ptr();
694f395db8SChristopher Di Bella free(ptr()); // no-warning
704f395db8SChristopher Di Bella }
714f395db8SChristopher Di Bella
t5b()724f395db8SChristopher Di Bella void t5b () {
734f395db8SChristopher Di Bella extern char *ptr();
744f395db8SChristopher Di Bella std::free(ptr()); // no-warning
754f395db8SChristopher Di Bella }
764f395db8SChristopher Di Bella
t6a()774f395db8SChristopher Di Bella void t6a () {
784f395db8SChristopher Di Bella free((void*)1000);
794f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
804f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object '(void *)1000'}}
814f395db8SChristopher Di Bella }
824f395db8SChristopher Di Bella
t6b()834f395db8SChristopher Di Bella void t6b () {
844f395db8SChristopher Di Bella std::free((void*)1000);
854f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
864f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object '(void *)1000'}}
874f395db8SChristopher Di Bella }
884f395db8SChristopher Di Bella
t7a(char ** x)894f395db8SChristopher Di Bella void t7a (char **x) {
904f395db8SChristopher Di Bella free(*x); // no-warning
914f395db8SChristopher Di Bella }
924f395db8SChristopher Di Bella
t7b(char ** x)934f395db8SChristopher Di Bella void t7b (char **x) {
944f395db8SChristopher Di Bella std::free(*x); // no-warning
954f395db8SChristopher Di Bella }
964f395db8SChristopher Di Bella
t8a(char ** x)974f395db8SChristopher Di Bella void t8a (char **x) {
984f395db8SChristopher Di Bella // ugh
994f395db8SChristopher Di Bella free((*x)+8); // no-warning
1004f395db8SChristopher Di Bella }
1014f395db8SChristopher Di Bella
t8b(char ** x)1024f395db8SChristopher Di Bella void t8b (char **x) {
1034f395db8SChristopher Di Bella // ugh
1044f395db8SChristopher Di Bella std::free((*x)+8); // no-warning
1054f395db8SChristopher Di Bella }
1064f395db8SChristopher Di Bella
t9a()1074f395db8SChristopher Di Bella void t9a () {
1084f395db8SChristopher Di Bella label:
1094f395db8SChristopher Di Bella free(&&label);
1104f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
1114f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object 'label'}}
1124f395db8SChristopher Di Bella }
1134f395db8SChristopher Di Bella
t9b()1144f395db8SChristopher Di Bella void t9b () {
1154f395db8SChristopher Di Bella label:
1164f395db8SChristopher Di Bella std::free(&&label);
1174f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
1184f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object 'label'}}
1194f395db8SChristopher Di Bella }
1204f395db8SChristopher Di Bella
t10a()1214f395db8SChristopher Di Bella void t10a () {
1224f395db8SChristopher Di Bella free((void*)&t10a);
1234f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the function 't10a', which is not memory allocated by malloc()}}
1244f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object 't10a'}}
1254f395db8SChristopher Di Bella }
1264f395db8SChristopher Di Bella
t10b()1274f395db8SChristopher Di Bella void t10b () {
1284f395db8SChristopher Di Bella std::free((void*)&t10b);
1294f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the function 't10b', which is not memory allocated by malloc()}}
1304f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object 't10b'}}
1314f395db8SChristopher Di Bella }
1324f395db8SChristopher Di Bella
t11a()1334f395db8SChristopher Di Bella void t11a () {
1344f395db8SChristopher Di Bella char *p = (char*)alloca(2);
1354f395db8SChristopher Di Bella free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
1364f395db8SChristopher Di Bella }
1374f395db8SChristopher Di Bella
t11b()1384f395db8SChristopher Di Bella void t11b () {
1394f395db8SChristopher Di Bella char *p = (char*)alloca(2);
1404f395db8SChristopher Di Bella std::free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
1414f395db8SChristopher Di Bella }
1424f395db8SChristopher Di Bella
t12a()1434f395db8SChristopher Di Bella void t12a () {
1444f395db8SChristopher Di Bella char *p = (char*)__builtin_alloca(2);
1454f395db8SChristopher Di Bella free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
1464f395db8SChristopher Di Bella }
1474f395db8SChristopher Di Bella
t12b()1484f395db8SChristopher Di Bella void t12b () {
1494f395db8SChristopher Di Bella char *p = (char*)__builtin_alloca(2);
1504f395db8SChristopher Di Bella std::free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
1514f395db8SChristopher Di Bella }
1524f395db8SChristopher Di Bella
t13a()1534f395db8SChristopher Di Bella void t13a () {
1544f395db8SChristopher Di Bella free(^{return;});
1554f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is a block, which is not memory allocated by malloc()}}
1564f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object: block expression}}
1574f395db8SChristopher Di Bella }
1584f395db8SChristopher Di Bella
t13b()1594f395db8SChristopher Di Bella void t13b () {
1604f395db8SChristopher Di Bella std::free(^{return;});
1614f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is a block, which is not memory allocated by malloc()}}
1624f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object: block expression}}
1634f395db8SChristopher Di Bella }
1644f395db8SChristopher Di Bella
t14a()1654f395db8SChristopher Di Bella void t14a () {
1664f395db8SChristopher Di Bella free((void *)+[]{ return; });
1674f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the function '__invoke', which is not memory allocated by malloc()}}
1684f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object: lambda-to-function-pointer conversion}}
1694f395db8SChristopher Di Bella }
1704f395db8SChristopher Di Bella
t14b()1714f395db8SChristopher Di Bella void t14b () {
1724f395db8SChristopher Di Bella std::free((void *)+[]{ return; });
1734f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the function '__invoke', which is not memory allocated by malloc()}}
1744f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object: lambda-to-function-pointer conversion}}
1754f395db8SChristopher Di Bella }
1764f395db8SChristopher Di Bella
t15a(char a)1774f395db8SChristopher Di Bella void t15a (char a) {
1784f395db8SChristopher Di Bella free(&a);
1794f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
1804f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
1814f395db8SChristopher Di Bella }
1824f395db8SChristopher Di Bella
t15b(char a)1834f395db8SChristopher Di Bella void t15b (char a) {
1844f395db8SChristopher Di Bella std::free(&a);
1854f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
1864f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
1874f395db8SChristopher Di Bella }
1884f395db8SChristopher Di Bella
1894f395db8SChristopher Di Bella static int someGlobal[2];
t16a()1904f395db8SChristopher Di Bella void t16a () {
1914f395db8SChristopher Di Bella free(someGlobal);
1924f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
1934f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call free on non-heap object 'someGlobal'}}
1944f395db8SChristopher Di Bella }
1954f395db8SChristopher Di Bella
t16b()1964f395db8SChristopher Di Bella void t16b () {
1974f395db8SChristopher Di Bella std::free(someGlobal);
1984f395db8SChristopher Di Bella // expected-warning@-1{{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
1994f395db8SChristopher Di Bella // expected-warning@-2{{attempt to call std::free on non-heap object 'someGlobal'}}
2004f395db8SChristopher Di Bella }
2014f395db8SChristopher Di Bella
t17a(char ** x,int offset)2024f395db8SChristopher Di Bella void t17a (char **x, int offset) {
2034f395db8SChristopher Di Bella // Unknown value
2044f395db8SChristopher Di Bella free(x[offset]); // no-warning
2054f395db8SChristopher Di Bella }
2064f395db8SChristopher Di Bella
t17b(char ** x,int offset)2074f395db8SChristopher Di Bella void t17b (char **x, int offset) {
2084f395db8SChristopher Di Bella // Unknown value
2094f395db8SChristopher Di Bella std::free(x[offset]); // no-warning
2104f395db8SChristopher Di Bella }
2119830901bSChristopher Di Bella
2129830901bSChristopher Di Bella struct S {
2139830901bSChristopher Di Bella const char* p;
2149830901bSChristopher Di Bella };
2159830901bSChristopher Di Bella
t18_C_style_C_style_free(S s)2169830901bSChristopher Di Bella void t18_C_style_C_style_free (S s) {
2179830901bSChristopher Di Bella free((void*)(unsigned long long)s.p); // no warning
2189830901bSChristopher Di Bella }
2199830901bSChristopher Di Bella
t18_C_style_C_style_std_free(S s)2209830901bSChristopher Di Bella void t18_C_style_C_style_std_free (S s) {
2219830901bSChristopher Di Bella std::free((void*)(unsigned long long)s.p); // no warning
2229830901bSChristopher Di Bella }
2239830901bSChristopher Di Bella
t18_C_style_reinterpret_free(S s)2249830901bSChristopher Di Bella void t18_C_style_reinterpret_free (S s) {
2259830901bSChristopher Di Bella free((void*)reinterpret_cast<unsigned long long>(s.p)); // no warning
2269830901bSChristopher Di Bella }
2279830901bSChristopher Di Bella
t18_C_style_reinterpret_std_free(S s)2289830901bSChristopher Di Bella void t18_C_style_reinterpret_std_free (S s) {
2299830901bSChristopher Di Bella std::free((void*)reinterpret_cast<unsigned long long>(s.p)); // no warning
2309830901bSChristopher Di Bella }
2319830901bSChristopher Di Bella
t18_reinterpret_C_style_free(S s)2329830901bSChristopher Di Bella void t18_reinterpret_C_style_free (S s) {
2339830901bSChristopher Di Bella free(reinterpret_cast<void*>((unsigned long long)(s.p))); // no warning
2349830901bSChristopher Di Bella }
2359830901bSChristopher Di Bella
t18_reinterpret_C_style_std_free(S s)2369830901bSChristopher Di Bella void t18_reinterpret_C_style_std_free (S s) {
2379830901bSChristopher Di Bella std::free(reinterpret_cast<void*>((unsigned long long)(s.p))); // no warning
2389830901bSChristopher Di Bella }
2399830901bSChristopher Di Bella
t18_reinterpret_reinterpret_free(S s)2409830901bSChristopher Di Bella void t18_reinterpret_reinterpret_free (S s) {
2419830901bSChristopher Di Bella free(reinterpret_cast<void*>(reinterpret_cast<unsigned long long>(s.p))); // no warning
2429830901bSChristopher Di Bella }
2439830901bSChristopher Di Bella
t18_reinterpret_reinterpret_std_free(S s)2449830901bSChristopher Di Bella void t18_reinterpret_reinterpret_std_free (S s) {
2459830901bSChristopher Di Bella std::free(reinterpret_cast<void*>(reinterpret_cast<unsigned long long>(s.p))); // no warning
2469830901bSChristopher Di Bella }
247