2009-11-14 02:46:29 +08:00
|
|
|
// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-experimental-checks -analyzer-store=region -verify %s
|
2009-11-14 12:23:25 +08:00
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
2009-11-14 04:03:22 +08:00
|
|
|
void *malloc(size_t);
|
|
|
|
void free(void *);
|
2009-11-13 15:48:11 +08:00
|
|
|
|
|
|
|
void f1() {
|
|
|
|
int *p = malloc(10);
|
|
|
|
return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
|
|
|
|
}
|
|
|
|
|
2009-11-14 03:53:32 +08:00
|
|
|
void f1_b() {
|
2009-11-17 15:54:15 +08:00
|
|
|
int *p = malloc(10); // expected-warning{{Allocated memory never released. Potential memory leak.}}
|
2009-11-14 03:53:32 +08:00
|
|
|
}
|
|
|
|
|
2009-11-13 15:48:11 +08:00
|
|
|
void f2() {
|
|
|
|
int *p = malloc(10);
|
|
|
|
free(p);
|
|
|
|
free(p); // expected-warning{{Try to free a memory block that has been released}}
|
|
|
|
}
|
2009-11-14 04:00:28 +08:00
|
|
|
|
2009-11-17 15:54:15 +08:00
|
|
|
// This case tests that storing malloc'ed memory to a static variable which is
|
|
|
|
// then returned is not leaked. In the absence of known contracts for functions
|
|
|
|
// or inter-procedural analysis, this is a conservative answer.
|
2009-11-14 04:00:28 +08:00
|
|
|
int *f3() {
|
|
|
|
static int *p = 0;
|
2009-11-17 15:54:15 +08:00
|
|
|
p = malloc(10); // will be fixed.
|
|
|
|
return p; // expected-warning{{Allocated memory never released. Potential memory leak.}}
|
2009-11-14 04:00:28 +08:00
|
|
|
}
|
|
|
|
|
2009-11-17 15:54:15 +08:00
|
|
|
// This case tests that storing malloc'ed memory to a static global variable
|
|
|
|
// which is then returned is not leaked. In the absence of known contracts for
|
|
|
|
// functions or inter-procedural analysis, this is a conservative answer.
|
2009-11-14 04:00:28 +08:00
|
|
|
static int *p_f4 = 0;
|
|
|
|
int *f4() {
|
2009-11-17 15:54:15 +08:00
|
|
|
p_f4 = malloc(10); // will be fixed.
|
|
|
|
return p_f4; // expected-warning{{Allocated memory never released. Potential memory leak.}}
|
2009-11-14 04:00:28 +08:00
|
|
|
}
|