2010-02-05 10:06:54 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -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-12-12 20:29:38 +08:00
|
|
|
void *realloc(void *ptr, size_t size);
|
|
|
|
void *calloc(size_t nmemb, size_t size);
|
2009-11-13 15:48:11 +08:00
|
|
|
|
|
|
|
void f1() {
|
2010-05-25 12:59:19 +08:00
|
|
|
int *p = malloc(12);
|
2009-11-13 15:48:11 +08:00
|
|
|
return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
|
|
|
|
}
|
|
|
|
|
2009-11-14 03:53:32 +08:00
|
|
|
void f1_b() {
|
2010-05-25 12:59:19 +08:00
|
|
|
int *p = malloc(12); // 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() {
|
2010-05-25 12:59:19 +08:00
|
|
|
int *p = malloc(12);
|
2009-11-13 15:48:11 +08:00
|
|
|
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;
|
2010-05-25 12:59:19 +08:00
|
|
|
p = malloc(12);
|
2009-11-17 16:58:18 +08:00
|
|
|
return p; // no-warning
|
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() {
|
2010-05-25 12:59:19 +08:00
|
|
|
p_f4 = malloc(12);
|
2009-11-17 16:58:18 +08:00
|
|
|
return p_f4; // no-warning
|
2009-11-14 04:00:28 +08:00
|
|
|
}
|
2009-12-12 20:29:38 +08:00
|
|
|
|
|
|
|
int *f5() {
|
2010-05-25 12:59:19 +08:00
|
|
|
int *q = malloc(12);
|
2009-12-12 20:29:38 +08:00
|
|
|
q = realloc(q, 20);
|
|
|
|
return q; // no-warning
|
|
|
|
}
|
2009-12-31 14:13:07 +08:00
|
|
|
|
|
|
|
void f6() {
|
2010-05-25 12:59:19 +08:00
|
|
|
int *p = malloc(12);
|
2009-12-31 14:13:07 +08:00
|
|
|
if (!p)
|
|
|
|
return; // no-warning
|
|
|
|
else
|
|
|
|
free(p);
|
|
|
|
}
|
2010-01-18 12:01:40 +08:00
|
|
|
|
|
|
|
char *doit2();
|
|
|
|
void pr6069() {
|
|
|
|
char *buf = doit2();
|
|
|
|
free(buf);
|
|
|
|
}
|
2010-02-14 14:49:48 +08:00
|
|
|
|
|
|
|
void pr6293() {
|
|
|
|
free(0);
|
|
|
|
}
|
2010-03-10 12:58:55 +08:00
|
|
|
|
|
|
|
void f7() {
|
|
|
|
char *x = (char*) malloc(4);
|
|
|
|
free(x);
|
|
|
|
x[0] = 'a'; // expected-warning{{Use dynamically allocated memory after it is freed.}}
|
|
|
|
}
|
2010-05-25 12:59:19 +08:00
|
|
|
|
|
|
|
void PR6123() {
|
|
|
|
int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PR7217() {
|
|
|
|
int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
|
|
|
|
buf[1] = 'c'; // not crash
|
2010-06-20 12:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mallocCastToVoid() {
|
|
|
|
void *p = malloc(2);
|
|
|
|
const void *cp = p; // not crash
|
|
|
|
free(p);
|
|
|
|
}
|
2010-05-25 12:59:19 +08:00
|
|
|
|
2010-06-20 12:30:57 +08:00
|
|
|
void mallocCastToFP() {
|
|
|
|
void *p = malloc(2);
|
|
|
|
void (*fp)() = p; // not crash
|
|
|
|
free(p);
|
2010-05-25 12:59:19 +08:00
|
|
|
}
|
2010-06-20 12:30:57 +08:00
|
|
|
|
2010-06-01 11:01:33 +08:00
|
|
|
// This tests that malloc() buffers are undefined by default
|
|
|
|
char mallocGarbage () {
|
|
|
|
char *buf = malloc(2);
|
|
|
|
char result = buf[1]; // expected-warning{{undefined}}
|
|
|
|
free(buf);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This tests that calloc() buffers need to be freed
|
|
|
|
void callocNoFree () {
|
|
|
|
char *buf = calloc(2,2);
|
|
|
|
return; // expected-warning{{never released}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These test that calloc() buffers are zeroed by default
|
|
|
|
char callocZeroesGood () {
|
|
|
|
char *buf = calloc(2,2);
|
|
|
|
char result = buf[3]; // no-warning
|
|
|
|
if (buf[1] == 0) {
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
return result; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
char callocZeroesBad () {
|
|
|
|
char *buf = calloc(2,2);
|
|
|
|
char result = buf[3]; // no-warning
|
|
|
|
if (buf[1] != 0) {
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
return result; // expected-warning{{never released}}
|
|
|
|
}
|