blob: 6a17cba3497bfd06e88022652bafc8ee32ebf38c [file] [log] [blame]
Ted Kremenek8382cf52009-11-13 18:46:29 +00001// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-experimental-checks -analyzer-store=region -verify %s
Eli Friedman2f005522009-11-14 04:23:25 +00002typedef __typeof(sizeof(int)) size_t;
Ted Kremenekc3607752009-11-13 20:03:22 +00003void *malloc(size_t);
4void free(void *);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +00005
6void f1() {
7 int *p = malloc(10);
8 return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
9}
10
Ted Kremenekba930872009-11-13 19:53:32 +000011void f1_b() {
Zhongxing Xu243fde92009-11-17 07:54:15 +000012 int *p = malloc(10); // expected-warning{{Allocated memory never released. Potential memory leak.}}
Ted Kremenekba930872009-11-13 19:53:32 +000013}
14
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000015void f2() {
16 int *p = malloc(10);
17 free(p);
18 free(p); // expected-warning{{Try to free a memory block that has been released}}
19}
Ted Kremenekc764d4b2009-11-13 20:00:28 +000020
Zhongxing Xu243fde92009-11-17 07:54:15 +000021// This case tests that storing malloc'ed memory to a static variable which is
22// then returned is not leaked. In the absence of known contracts for functions
23// or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +000024int *f3() {
25 static int *p = 0;
Zhongxing Xu4985e3e2009-11-17 08:58:18 +000026 p = malloc(10);
27 return p; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +000028}
29
Zhongxing Xu243fde92009-11-17 07:54:15 +000030// This case tests that storing malloc'ed memory to a static global variable
31// which is then returned is not leaked. In the absence of known contracts for
32// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +000033static int *p_f4 = 0;
34int *f4() {
Zhongxing Xu4985e3e2009-11-17 08:58:18 +000035 p_f4 = malloc(10);
36 return p_f4; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +000037}