[cfe-commits] r132956 - /cfe/trunk/lib/StaticAnalyzer/Checkers/CStringChecker.cpp

Jordy Rose jediknil at belkadan.com
Mon Jun 13 18:26:48 PDT 2011


Author: jrose
Date: Mon Jun 13 20:26:48 2011
New Revision: 132956

URL: http://llvm.org/viewvc/llvm-project?rev=132956&view=rev
Log:
[analyzer] Change large if body to early return. No functionality change.

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

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/CStringChecker.cpp?rev=132956&r1=132955&r2=132956&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/CStringChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/CStringChecker.cpp Mon Jun 13 20:26:48 2011
@@ -934,92 +934,93 @@
 
   state = checkNonNull(C, state, Arg, ArgVal);
 
-  if (state) {
-    SVal strLength = getCStringLength(C, state, Arg, ArgVal);
+  if (!state)
+    return;
 
-    // If the argument isn't a valid C string, there's no valid state to
-    // transition to.
-    if (strLength.isUndef())
-      return;
+  SVal strLength = getCStringLength(C, state, Arg, ArgVal);
+
+  // If the argument isn't a valid C string, there's no valid state to
+  // transition to.
+  if (strLength.isUndef())
+    return;
 
-    DefinedOrUnknownSVal result = UnknownVal();
+  DefinedOrUnknownSVal result = UnknownVal();
 
-    // If the check is for strnlen() then bind the return value to no more than
-    // the maxlen value.
-    if (IsStrnlen) {
-      QualType cmpTy = C.getSValBuilder().getContext().IntTy;
-
-      // It's a little unfortunate to be getting this again,
-      // but it's not that expensive...
-      const Expr *maxlenExpr = CE->getArg(1);
-      SVal maxlenVal = state->getSVal(maxlenExpr);
-
-      NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
-      NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
-
-      if (strLengthNL && maxlenValNL) {
-        const GRState *stateStringTooLong, *stateStringNotTooLong;
-
-        // Check if the strLength is greater than the maxlen.
-        llvm::tie(stateStringTooLong, stateStringNotTooLong) =
-          state->assume(cast<DefinedOrUnknownSVal>
-                        (C.getSValBuilder().evalBinOpNN(state, BO_GT, 
-                                                        *strLengthNL,
-                                                        *maxlenValNL,
-                                                        cmpTy)));
-
-        if (stateStringTooLong && !stateStringNotTooLong) {
-          // If the string is longer than maxlen, return maxlen.
-          result = *maxlenValNL;
-        } else if (stateStringNotTooLong && !stateStringTooLong) {
-          // If the string is shorter than maxlen, return its length.
-          result = *strLengthNL;
-        }
-      }
+  // If the check is for strnlen() then bind the return value to no more than
+  // the maxlen value.
+  if (IsStrnlen) {
+    QualType cmpTy = C.getSValBuilder().getContext().IntTy;
 
-      if (result.isUnknown()) {
-        // If we don't have enough information for a comparison, there's
-        // no guarantee the full string length will actually be returned.
-        // All we know is the return value is the min of the string length
-        // and the limit. This is better than nothing.
-        unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
-        result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
-        NonLoc *resultNL = cast<NonLoc>(&result);
-
-        if (strLengthNL) {
-          state = state->assume(cast<DefinedOrUnknownSVal>
-                                (C.getSValBuilder().evalBinOpNN(state, BO_LE, 
-                                                                *resultNL,
-                                                                *strLengthNL,
-                                                                cmpTy)), true);
-        }
-        
-        if (maxlenValNL) {
-          state = state->assume(cast<DefinedOrUnknownSVal>
-                                (C.getSValBuilder().evalBinOpNN(state, BO_LE, 
-                                                                *resultNL,
-                                                                *maxlenValNL,
-                                                                cmpTy)), true);
-        }
+    // It's a little unfortunate to be getting this again,
+    // but it's not that expensive...
+    const Expr *maxlenExpr = CE->getArg(1);
+    SVal maxlenVal = state->getSVal(maxlenExpr);
+
+    NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
+    NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
+
+    if (strLengthNL && maxlenValNL) {
+      const GRState *stateStringTooLong, *stateStringNotTooLong;
+
+      // Check if the strLength is greater than the maxlen.
+      llvm::tie(stateStringTooLong, stateStringNotTooLong) =
+        state->assume(cast<DefinedOrUnknownSVal>
+                      (C.getSValBuilder().evalBinOpNN(state, BO_GT, 
+                                                      *strLengthNL,
+                                                      *maxlenValNL,
+                                                      cmpTy)));
+
+      if (stateStringTooLong && !stateStringNotTooLong) {
+        // If the string is longer than maxlen, return maxlen.
+        result = *maxlenValNL;
+      } else if (stateStringNotTooLong && !stateStringTooLong) {
+        // If the string is shorter than maxlen, return its length.
+        result = *strLengthNL;
       }
+    }
 
-    } else {
-      // This is a plain strlen(), not strnlen().
-      result = cast<DefinedOrUnknownSVal>(strLength);
-
-      // If we don't know the length of the string, conjure a return
-      // value, so it can be used in constraints, at least.
-      if (result.isUnknown()) {
-        unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
-        result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
+    if (result.isUnknown()) {
+      // If we don't have enough information for a comparison, there's
+      // no guarantee the full string length will actually be returned.
+      // All we know is the return value is the min of the string length
+      // and the limit. This is better than nothing.
+      unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
+      result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
+      NonLoc *resultNL = cast<NonLoc>(&result);
+
+      if (strLengthNL) {
+        state = state->assume(cast<DefinedOrUnknownSVal>
+                              (C.getSValBuilder().evalBinOpNN(state, BO_LE, 
+                                                              *resultNL,
+                                                              *strLengthNL,
+                                                              cmpTy)), true);
+      }
+      
+      if (maxlenValNL) {
+        state = state->assume(cast<DefinedOrUnknownSVal>
+                              (C.getSValBuilder().evalBinOpNN(state, BO_LE, 
+                                                              *resultNL,
+                                                              *maxlenValNL,
+                                                              cmpTy)), true);
       }
     }
 
-    // Bind the return value.
-    assert(!result.isUnknown() && "Should have conjured a value by now");
-    state = state->BindExpr(CE, result);
-    C.addTransition(state);
+  } else {
+    // This is a plain strlen(), not strnlen().
+    result = cast<DefinedOrUnknownSVal>(strLength);
+
+    // If we don't know the length of the string, conjure a return
+    // value, so it can be used in constraints, at least.
+    if (result.isUnknown()) {
+      unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
+      result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
+    }
   }
+
+  // Bind the return value.
+  assert(!result.isUnknown() && "Should have conjured a value by now");
+  state = state->BindExpr(CE, result);
+  C.addTransition(state);
 }
 
 void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {





More information about the cfe-commits mailing list