2011-08-04 07:14:55 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,experimental.core.CastSize,experimental.unix.Malloc -analyzer-store=region -verify %s
|
2012-02-12 07:46:36 +08:00
|
|
|
#include "system-header-simulator.h"
|
|
|
|
|
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);
|
2012-02-09 07:16:56 +08:00
|
|
|
|
|
|
|
void myfoo(int *p);
|
|
|
|
void myfooint(int p);
|
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.}}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-04-27 22:49:29 +08:00
|
|
|
void f2_realloc_0() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
realloc(p,0);
|
|
|
|
realloc(p,0); // expected-warning{{Try to free a memory block that has been released}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f2_realloc_1() {
|
|
|
|
int *p = malloc(12);
|
2011-09-01 12:53:59 +08:00
|
|
|
int *q = realloc(p,0); // no-warning
|
2011-04-27 22:49:29 +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
|
|
|
|
2011-04-27 22:49:29 +08:00
|
|
|
void f6_realloc() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
if (!p)
|
|
|
|
return; // no-warning
|
|
|
|
else
|
|
|
|
realloc(p,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2012-02-10 09:11:00 +08:00
|
|
|
x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
|
2010-03-10 12:58:55 +08:00
|
|
|
}
|
2010-05-25 12:59:19 +08:00
|
|
|
|
2011-04-27 22:49:29 +08:00
|
|
|
void f7_realloc() {
|
|
|
|
char *x = (char*) malloc(4);
|
|
|
|
realloc(x,0);
|
2012-02-10 09:11:00 +08:00
|
|
|
x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
|
2011-04-27 22:49:29 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2010-07-24 07:04:53 +08:00
|
|
|
free(buf); // expected-warning{{never executed}}
|
2010-06-01 11:01:33 +08:00
|
|
|
}
|
|
|
|
return result; // expected-warning{{never released}}
|
|
|
|
}
|
2012-02-09 07:16:56 +08:00
|
|
|
|
|
|
|
void nullFree() {
|
|
|
|
int *p = 0;
|
|
|
|
free(p); // no warning - a nop
|
|
|
|
}
|
|
|
|
|
|
|
|
void paramFree(int *p) {
|
|
|
|
myfoo(p);
|
|
|
|
free(p); // no warning
|
|
|
|
myfoo(p); // TODO: This should be a warning.
|
|
|
|
}
|
|
|
|
|
|
|
|
int* mallocEscapeRet() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
return p; // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocEscapeFoo() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
myfoo(p);
|
|
|
|
return; // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocEscapeFree() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
myfoo(p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocEscapeFreeFree() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
myfoo(p);
|
|
|
|
free(p);
|
|
|
|
free(p); // expected-warning{{Try to free a memory block that has been released}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocEscapeFreeUse() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
myfoo(p);
|
|
|
|
free(p);
|
2012-02-10 09:11:00 +08:00
|
|
|
myfoo(p); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
|
2012-02-09 07:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int *myalloc();
|
|
|
|
void myalloc2(int **p);
|
|
|
|
|
|
|
|
void mallocEscapeFreeCustomAlloc() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
myfoo(p);
|
|
|
|
free(p);
|
|
|
|
p = myalloc();
|
|
|
|
free(p); // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocEscapeFreeCustomAlloc2() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
myfoo(p);
|
|
|
|
free(p);
|
|
|
|
myalloc2(&p);
|
|
|
|
free(p); // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocBindFreeUse() {
|
|
|
|
int *x = malloc(12);
|
|
|
|
int *y = x;
|
|
|
|
free(y);
|
2012-02-10 09:11:00 +08:00
|
|
|
myfoo(x); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
|
2012-02-09 07:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mallocEscapeMalloc() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
myfoo(p);
|
|
|
|
p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocMalloc() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mallocFreeMalloc() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
free(p);
|
|
|
|
p = malloc(12);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2012-02-09 14:25:47 +08:00
|
|
|
void mallocFreeUse_params() {
|
2012-02-09 07:16:56 +08:00
|
|
|
int *p = malloc(12);
|
|
|
|
free(p);
|
2012-02-10 09:11:00 +08:00
|
|
|
myfoo(p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
|
2012-02-12 07:46:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mallocFreeUse_params2() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
free(p);
|
2012-02-10 09:11:00 +08:00
|
|
|
myfooint(*p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
|
2012-02-09 07:16:56 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
void mallocFailedOrNot() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
if (!p)
|
|
|
|
free(p);
|
|
|
|
else
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2012-02-10 09:11:00 +08:00
|
|
|
struct StructWithInt {
|
|
|
|
int g;
|
|
|
|
};
|
2012-02-12 05:44:39 +08:00
|
|
|
|
|
|
|
int *mallocReturnFreed() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
free(p);
|
|
|
|
return p; // expected-warning {{Use of dynamically allocated}}
|
|
|
|
}
|
|
|
|
|
|
|
|
int useAfterFreeStruct() {
|
|
|
|
struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
|
|
|
|
px->g = 5;
|
|
|
|
free(px);
|
|
|
|
return px->g; // expected-warning {{Use of dynamically allocated}}
|
|
|
|
}
|
|
|
|
|
2012-02-10 09:11:00 +08:00
|
|
|
void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
|
|
|
|
|
|
|
|
void mallocEscapeFooNonSymbolArg() {
|
|
|
|
struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
|
|
|
|
nonSymbolAsFirstArg(&p->g, p);
|
|
|
|
return; // no warning
|
|
|
|
}
|
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
void mallocFailedOrNotLeak() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
if (p == 0)
|
|
|
|
return; // no warning
|
|
|
|
else
|
|
|
|
return; // expected-warning {{Allocated memory never released. Potential memory leak.}}
|
|
|
|
}
|
2012-02-10 09:11:00 +08:00
|
|
|
|
2012-02-09 14:25:47 +08:00
|
|
|
int *Gl;
|
|
|
|
struct GlStTy {
|
|
|
|
int *x;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GlStTy GlS = {0};
|
|
|
|
|
|
|
|
void GlobalFree() {
|
|
|
|
free(Gl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalMalloc() {
|
|
|
|
Gl = malloc(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalStructMalloc() {
|
|
|
|
int *a = malloc(12);
|
|
|
|
GlS.x = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalStructMallocFree() {
|
|
|
|
int *a = malloc(12);
|
|
|
|
GlS.x = a;
|
|
|
|
free(GlS.x);
|
|
|
|
}
|
2012-02-10 09:11:03 +08:00
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
// Region escape testing.
|
|
|
|
|
|
|
|
unsigned takePtrToPtr(int **p);
|
|
|
|
void PassTheAddrOfAllocatedData(int f) {
|
|
|
|
int *p = malloc(12);
|
|
|
|
// We don't know what happens after the call. Should stop tracking here.
|
|
|
|
if (takePtrToPtr(&p))
|
|
|
|
f++;
|
|
|
|
free(p); // no warning
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X {
|
|
|
|
int *p;
|
|
|
|
};
|
|
|
|
unsigned takePtrToStruct(struct X *s);
|
|
|
|
int ** foo2(int *g, int f) {
|
|
|
|
int *p = malloc(12);
|
|
|
|
struct X *px= malloc(sizeof(struct X));
|
|
|
|
px->p = p;
|
|
|
|
// We don't know what happens after this call. Should not track px nor p.
|
|
|
|
if (takePtrToStruct(px))
|
|
|
|
f++;
|
|
|
|
free(p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X* RegInvalidationDetect1(struct X *s2) {
|
|
|
|
struct X *px= malloc(sizeof(struct X));
|
|
|
|
px->p = 0;
|
|
|
|
px = s2;
|
|
|
|
return px; // expected-warning {{Allocated memory never released. Potential memory leak.}}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X* RegInvalidationGiveUp1() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
struct X *px= malloc(sizeof(struct X));
|
|
|
|
px->p = p;
|
|
|
|
return px;
|
|
|
|
}
|
|
|
|
|
|
|
|
int **RegInvalidationDetect2(int **pp) {
|
|
|
|
int *p = malloc(12);
|
|
|
|
pp = &p;
|
|
|
|
pp++;
|
|
|
|
return 0;// expected-warning {{Allocated memory never released. Potential memory leak.}}
|
|
|
|
}
|
2012-02-10 09:11:03 +08:00
|
|
|
|
|
|
|
extern void exit(int) __attribute__ ((__noreturn__));
|
|
|
|
void mallocExit(int *g) {
|
|
|
|
struct xx *p = malloc(12);
|
2012-02-12 05:02:40 +08:00
|
|
|
if (g != 0)
|
|
|
|
exit(1);
|
2012-02-10 09:11:03 +08:00
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void __assert_fail (__const char *__assertion, __const char *__file,
|
|
|
|
unsigned int __line, __const char *__function)
|
|
|
|
__attribute__ ((__noreturn__));
|
|
|
|
#define assert(expr) \
|
|
|
|
((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
|
|
|
|
void mallocAssert(int *g) {
|
|
|
|
struct xx *p = malloc(12);
|
|
|
|
|
2012-02-12 05:02:40 +08:00
|
|
|
assert(g != 0);
|
2012-02-10 09:11:03 +08:00
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-12 07:46:36 +08:00
|
|
|
void doNotInvalidateWhenPassedToSystemCalls(char *s) {
|
|
|
|
char *p = malloc(12);
|
|
|
|
strlen(p);
|
|
|
|
strcpy(p, s); // expected-warning {{leak}}
|
|
|
|
}
|
|
|
|
|
2012-02-12 05:02:40 +08:00
|
|
|
// Below are the known false positives.
|
|
|
|
|
2012-02-10 09:11:03 +08:00
|
|
|
// TODO: There should be no warning here.
|
|
|
|
void reallocFails(int *g, int f) {
|
|
|
|
char *p = malloc(12);
|
|
|
|
char *r = realloc(p, 12+1);
|
|
|
|
if (!r) {
|
|
|
|
free(p); // expected-warning {{Try to free a memory block that has been released}}
|
|
|
|
} else {
|
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: There should be no warning here. This one might be difficult to get rid of.
|
|
|
|
void dependsOnValueOfPtr(int *g, unsigned f) {
|
|
|
|
int *p;
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
p = g;
|
|
|
|
} else {
|
|
|
|
p = malloc(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != g)
|
|
|
|
free(p);
|
|
|
|
else
|
|
|
|
return; // expected-warning{{Allocated memory never released. Potential memory leak}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Should this be a warning?
|
|
|
|
// Here we are returning a pointer one past the allocated value. An idiom which
|
|
|
|
// can be used for implementing special malloc. The correct uses of this might
|
|
|
|
// be rare enough so that we could keep this as a warning.
|
|
|
|
static void *specialMalloc(int n){
|
|
|
|
int *p;
|
|
|
|
p = malloc( n+8 );
|
|
|
|
if( p ){
|
|
|
|
p[0] = n;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return p;// expected-warning {{Allocated memory never released. Potential memory leak.}}
|
|
|
|
}
|