blob: 4d771eeb4bd3d13c28b3a96eb6630671a63ad611 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-experimental-checks -analyzer-store=region -verify %s
Eli Friedmanb7746852009-11-14 04:23:25 +00002typedef __typeof(sizeof(int)) size_t;
Ted Kremenek9430bf22009-11-13 20:03:22 +00003void *malloc(size_t);
4void free(void *);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +00005void *realloc(void *ptr, size_t size);
6void *calloc(size_t nmemb, size_t size);
Zhongxing Xuc7460962009-11-13 07:48:11 +00007
8void f1() {
9 int *p = malloc(10);
10 return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
11}
12
Ted Kremenekc2675562009-11-13 19:53:32 +000013void f1_b() {
Zhongxing Xu4668c7e2009-11-17 07:54:15 +000014 int *p = malloc(10); // expected-warning{{Allocated memory never released. Potential memory leak.}}
Ted Kremenekc2675562009-11-13 19:53:32 +000015}
16
Zhongxing Xuc7460962009-11-13 07:48:11 +000017void f2() {
18 int *p = malloc(10);
19 free(p);
20 free(p); // expected-warning{{Try to free a memory block that has been released}}
21}
Ted Kremeneke5e977012009-11-13 20:00:28 +000022
Zhongxing Xu4668c7e2009-11-17 07:54:15 +000023// This case tests that storing malloc'ed memory to a static variable which is
24// then returned is not leaked. In the absence of known contracts for functions
25// or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +000026int *f3() {
27 static int *p = 0;
Zhongxing Xu23baa012009-11-17 08:58:18 +000028 p = malloc(10);
29 return p; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +000030}
31
Zhongxing Xu4668c7e2009-11-17 07:54:15 +000032// This case tests that storing malloc'ed memory to a static global variable
33// which is then returned is not leaked. In the absence of known contracts for
34// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +000035static int *p_f4 = 0;
36int *f4() {
Zhongxing Xu23baa012009-11-17 08:58:18 +000037 p_f4 = malloc(10);
38 return p_f4; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +000039}
Zhongxing Xuc0484fa2009-12-12 12:29:38 +000040
41int *f5() {
42 int *q = malloc(10);
43 q = realloc(q, 20);
44 return q; // no-warning
45}
Zhongxing Xub0e15df2009-12-31 06:13:07 +000046
47void f6() {
48 int *p = malloc(10);
49 if (!p)
50 return; // no-warning
51 else
52 free(p);
53}