diff options
author | Anna Zaks <ganna@apple.com> | 2012-02-10 01:11:03 +0000 |
---|---|---|
committer | Anna Zaks <ganna@apple.com> | 2012-02-10 01:11:03 +0000 |
commit | e963fd51a84731bf1dfa4a2d757b3459dd7cfeb5 (patch) | |
tree | a42318fcaaf6491d63e38b23642aa6caa353d724 /clang/test/Analysis/malloc.c | |
parent | 3188686c55bc5841b7403fe147f841b5d3736ad4 (diff) | |
download | bcm5719-llvm-e963fd51a84731bf1dfa4a2d757b3459dd7cfeb5.tar.gz bcm5719-llvm-e963fd51a84731bf1dfa4a2d757b3459dd7cfeb5.zip |
[analyzer] MallocChecker: add a list of false positives based on running
the checker over postgres and sqlite.
llvm-svn: 150216
Diffstat (limited to 'clang/test/Analysis/malloc.c')
-rw-r--r-- | clang/test/Analysis/malloc.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/clang/test/Analysis/malloc.c b/clang/test/Analysis/malloc.c index 8d6295609b9..ec767050be5 100644 --- a/clang/test/Analysis/malloc.c +++ b/clang/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.}} +} |