[cfe-commits] r136852 - /cfe/trunk/lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp

Anna Zaks ganna at apple.com
Wed Aug 3 17:31:38 PDT 2011


Author: zaks
Date: Wed Aug  3 19:31:38 2011
New Revision: 136852

URL: http://llvm.org/viewvc/llvm-project?rev=136852&view=rev
Log:
KeychainAPI checker: refactor to use early exit.

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp?rev=136852&r1=136851&r2=136852&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp Wed Aug  3 19:31:38 2011
@@ -116,33 +116,34 @@
 
   // If a value has been freed, remove from the list.
   unsigned idx = getDeallocatingFunctionParam(funName);
-  if (idx != InvalidParamVal) {
-    const Expr *ArgExpr = CE->getArg(idx);
-    const MemRegion *Arg = State->getSVal(ArgExpr).getAsRegion();
-    if (!Arg)
-      return;
+  if (idx == InvalidParamVal)
+    return;
 
-    // If trying to free data which has not been allocated yet, report as bug.
-    if (State->get<AllocatedData>(Arg) == 0) {
-      // It is possible that this is a false positive - the argument might
-      // have entered as an enclosing function parameter.
-      if (isEnclosingFunctionParam(ArgExpr))
-        return;
-
-      ExplodedNode *N = C.generateNode(State);
-      if (!N)
-        return;
-      initBugType();
-      RangedBugReport *Report = new RangedBugReport(*BT,
-          "Trying to free data which has not been allocated.", N);
-      Report->addRange(ArgExpr->getSourceRange());
-      C.EmitReport(Report);
-    }
+  const Expr *ArgExpr = CE->getArg(idx);
+  const MemRegion *Arg = State->getSVal(ArgExpr).getAsRegion();
+  if (!Arg)
+    return;
 
-    // Continue exploring from the new state.
-    State = State->remove<AllocatedData>(Arg);
-    C.addTransition(State);
+  // If trying to free data which has not been allocated yet, report as bug.
+  if (State->get<AllocatedData>(Arg) == 0) {
+    // It is possible that this is a false positive - the argument might
+    // have entered as an enclosing function parameter.
+    if (isEnclosingFunctionParam(ArgExpr))
+      return;
+
+    ExplodedNode *N = C.generateNode(State);
+    if (!N)
+      return;
+    initBugType();
+    RangedBugReport *Report = new RangedBugReport(*BT,
+        "Trying to free data which has not been allocated.", N);
+    Report->addRange(ArgExpr->getSourceRange());
+    C.EmitReport(Report);
   }
+
+  // Continue exploring from the new state.
+  State = State->remove<AllocatedData>(Arg);
+  C.addTransition(State);
 }
 
 void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE,
@@ -162,32 +163,33 @@
 
   // If a value has been allocated, add it to the set for tracking.
   unsigned idx = getAllocatingFunctionParam(funName);
-  if (idx != InvalidParamVal) {
-    SVal Arg = State->getSVal(CE->getArg(idx));
-    if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&Arg)) {
-      // Add the symbolic value, which represents the location of the allocated
-      // data, to the set.
-      const MemRegion *V = SM.Retrieve(State->getStore(), *X).getAsRegion();
-      // If this is not a region, it can be:
-      //  - unknown (cannot reason about it)
-      //  - undefined (already reported by other checker)
-      //  - constant (null - should not be tracked, other - report a warning?)
-      //  - goto (should be reported by other checker)
-      if (!V)
-        return;
-
-      State = State->set<AllocatedData>(V, AllocationInfo(CE->getArg(idx)));
-
-      // We only need to track the value if the function returned noErr(0), so
-      // bind the return value of the function to 0.
-      SValBuilder &Builder = C.getSValBuilder();
-      SVal ZeroVal = Builder.makeZeroVal(Builder.getContext().CharTy);
-      State = State->BindExpr(CE, ZeroVal);
-      assert(State);
-
-      // Proceed from the new state.
-      C.addTransition(State);
-    }
+  if (idx == InvalidParamVal)
+    return;
+
+  SVal Arg = State->getSVal(CE->getArg(idx));
+  if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&Arg)) {
+    // Add the symbolic value, which represents the location of the allocated
+    // data, to the set.
+    const MemRegion *V = SM.Retrieve(State->getStore(), *X).getAsRegion();
+    // If this is not a region, it can be:
+    //  - unknown (cannot reason about it)
+    //  - undefined (already reported by other checker)
+    //  - constant (null - should not be tracked, other - report a warning?)
+    //  - goto (should be reported by other checker)
+    if (!V)
+      return;
+
+    State = State->set<AllocatedData>(V, AllocationInfo(CE->getArg(idx)));
+
+    // We only need to track the value if the function returned noErr(0), so
+    // bind the return value of the function to 0.
+    SValBuilder &Builder = C.getSValBuilder();
+    SVal ZeroVal = Builder.makeZeroVal(Builder.getContext().CharTy);
+    State = State->BindExpr(CE, ZeroVal);
+    assert(State);
+
+    // Proceed from the new state.
+    C.addTransition(State);
   }
 }
 





More information about the cfe-commits mailing list