[cfe-commits] r89071 - in /cfe/trunk: lib/Analysis/MallocChecker.cpp test/Analysis/malloc.c

Zhongxing Xu xuzhongxing at gmail.com
Tue Nov 17 00:58:18 PST 2009


Author: zhongxingxu
Date: Tue Nov 17 02:58:18 2009
New Revision: 89071

URL: http://llvm.org/viewvc/llvm-project?rev=89071&view=rev
Log:
Add PreVisitReturn to Malloc checker. Now we can recognize returned memory
block.

Modified:
    cfe/trunk/lib/Analysis/MallocChecker.cpp
    cfe/trunk/test/Analysis/malloc.c

Modified: cfe/trunk/lib/Analysis/MallocChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/MallocChecker.cpp?rev=89071&r1=89070&r2=89071&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/MallocChecker.cpp (original)
+++ cfe/trunk/lib/Analysis/MallocChecker.cpp Tue Nov 17 02:58:18 2009
@@ -60,6 +60,7 @@
   void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE);
   void EvalDeadSymbols(CheckerContext &C,const Stmt *S,SymbolReaper &SymReaper);
   void EvalEndPath(GREndPathNodeBuilder &B, void *tag, GRExprEngine &Eng);
+  void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S);
 private:
   void MallocMem(CheckerContext &C, const CallExpr *CE);
   void FreeMem(CheckerContext &C, const CallExpr *CE);
@@ -190,3 +191,28 @@
     }
   }
 }
+
+void MallocChecker::PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S) {
+  const Expr *RetE = S->getRetValue();
+  if (!RetE)
+    return;
+
+  const GRState *state = C.getState();
+
+  SymbolRef Sym = state->getSVal(RetE).getAsSymbol();
+
+  if (!Sym)
+    return;
+
+  const RefState *RS = state->get<RegionState>(Sym);
+  if (!RS)
+    return;
+
+  // FIXME: check other cases.
+  if (RS->isAllocated())
+    state = state->set<RegionState>(Sym, RefState::getEscaped(S));
+
+  ExplodedNode *N = C.GenerateNode(S, state);
+  if (N)
+    C.addTransition(N);
+}

Modified: cfe/trunk/test/Analysis/malloc.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/malloc.c?rev=89071&r1=89070&r2=89071&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/malloc.c (original)
+++ cfe/trunk/test/Analysis/malloc.c Tue Nov 17 02:58:18 2009
@@ -23,8 +23,8 @@
 // or inter-procedural analysis, this is a conservative answer.
 int *f3() {
   static int *p = 0;
-  p = malloc(10); // will be fixed.
-  return p; // expected-warning{{Allocated memory never released. Potential memory leak.}}
+  p = malloc(10); 
+  return p; // no-warning
 }
 
 // This case tests that storing malloc'ed memory to a static global variable
@@ -32,6 +32,6 @@
 // functions or inter-procedural analysis, this is a conservative answer.
 static int *p_f4 = 0;
 int *f4() {
-  p_f4 = malloc(10); // will be fixed.
-  return p_f4; // expected-warning{{Allocated memory never released. Potential memory leak.}}
+  p_f4 = malloc(10); 
+  return p_f4; // no-warning
 }





More information about the cfe-commits mailing list