[cfe-commits] r150216 - /cfe/trunk/test/Analysis/malloc.c

Anna Zaks ganna at apple.com
Thu Feb 9 17:11:03 PST 2012


Author: zaks
Date: Thu Feb  9 19:11:03 2012
New Revision: 150216

URL: http://llvm.org/viewvc/llvm-project?rev=150216&view=rev
Log:
[analyzer] MallocChecker: add a list of false positives based on running
the checker over postgres and sqlite.

Modified:
    cfe/trunk/test/Analysis/malloc.c

Modified: cfe/trunk/test/Analysis/malloc.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/malloc.c?rev=150216&r1=150215&r2=150216&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/malloc.c (original)
+++ cfe/trunk/test/Analysis/malloc.c Thu Feb  9 19:11:03 2012
@@ -285,3 +285,85 @@
   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.}}
+}





More information about the cfe-commits mailing list