]> granicus.if.org Git - clang/commitdiff
[analyzer] MallocChecker: add a list of false positives based on running
authorAnna Zaks <ganna@apple.com>
Fri, 10 Feb 2012 01:11:03 +0000 (01:11 +0000)
committerAnna Zaks <ganna@apple.com>
Fri, 10 Feb 2012 01:11:03 +0000 (01:11 +0000)
the checker over postgres and sqlite.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150216 91177308-0d34-0410-b5e6-96231b3b80d8

test/Analysis/malloc.c

index 8d6295609b9430317fb7d4e50b45289db16f24e2..ec767050be5beeb59175c001d979062a94ab6082 100644 (file)
@@ -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.}}
+}