From: Anna Zaks Date: Fri, 10 Feb 2012 01:11:03 +0000 (+0000) Subject: [analyzer] MallocChecker: add a list of false positives based on running X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f8b1c316cb294d4d47579fbdf7d97d3260e2ba6e;p=clang [analyzer] MallocChecker: add a list of false positives based on running the checker over postgres and sqlite. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150216 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/Analysis/malloc.c b/test/Analysis/malloc.c index 8d6295609b..ec767050be 100644 --- a/test/Analysis/malloc.c +++ b/test/Analysis/malloc.c @@ -285,3 +285,85 @@ void GlobalStructMallocFree() { GlS.x = a; free(GlS.x); } + + +// Below are the known false positives. + +// TODO: There should be no warning here. +extern void exit(int) __attribute__ ((__noreturn__)); +void mallocExit(int *g) { + struct xx *p = malloc(12); + + if (g != 0) { + exit(1); // expected-warning{{Allocated memory never released. Potential memory leak}} + } + free(p); + return; +} + + +// TODO: There should be no warning here. +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); + + assert(g != 0); // expected-warning{{Allocated memory never released. Potential memory leak}} + free(p); + return; +} + +// TODO: There should be no warning here. +unsigned takePtrToPtr(int **p); +void PassTheAddrOfAllocatedData(int *g, int f) { + int *p = malloc(12); + // This call is causing the problem. + if (takePtrToPtr(&p)) + f++; // expected-warning{{Allocated memory never released. Potential memory leak}} + free(p); // expected-warning{{Allocated memory never released. Potential memory leak}} +} + +// 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.}} +}