]> granicus.if.org Git - clang/commitdiff
Fix: <rdar://problem/6776949> Branch condition evaluates to an uninitialized value...
authorTed Kremenek <kremenek@apple.com>
Fri, 10 Apr 2009 00:59:50 +0000 (00:59 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 10 Apr 2009 00:59:50 +0000 (00:59 +0000)
The analyzer now adds the precondition that the first argument of 'main' is > 0.

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

lib/Analysis/GRExprEngine.cpp
test/Analysis/misc-ps.m

index 1d1100a55ad61df8b72c73f93b0dc9ff6936b634..46d1f798cf0f436a4d6009465f6bfed479881f3a 100644 (file)
@@ -161,7 +161,31 @@ void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
 }
 
 const GRState* GRExprEngine::getInitialState() {
-  return StateMgr.getInitialState();
+  const GRState *state = StateMgr.getInitialState();
+  
+  // Precondition: the first argument of 'main' is an integer guaranteed
+  //  to be > 0.
+  // FIXME: It would be nice if we had a more general mechanism to add
+  // such preconditions.  Some day.
+  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
+    if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
+        FD->getNumParams() > 0) {
+      const ParmVarDecl *PD = FD->getParamDecl(0);
+      QualType T = PD->getType();
+      if (T->isIntegerType())
+        if (const MemRegion *R = StateMgr.getRegion(PD)) {
+          SVal V = GetSVal(state, loc::MemRegionVal(R));
+          SVal Constraint = EvalBinOp(BinaryOperator::GT, V,
+                                      ValMgr.makeZeroVal(T),
+                                      getContext().IntTy);          
+          bool isFeasible = false;          
+          const GRState *newState = Assume(state, Constraint, true,
+                                           isFeasible);
+          if (newState) state = newState;
+        }
+    }
+  
+  return state;
 }
 
 //===----------------------------------------------------------------------===//
index 777784aabcbe55077629c8bd4a2e779cbeb5659b..0f85d22436a4b545344f24a71d6267f77d58f2c6 100644 (file)
@@ -245,3 +245,25 @@ void rdar_6777003(int x) {
   *p = 1; // expected-warning{{Dereference of null pointer}}  
 }
 
+// <rdar://problem/6776949>
+// main's 'argc' argument is always > 0
+int main(int argc, char* argv[]) {
+  int *p = 0;
+
+  if (argc == 0)
+    *p = 1;
+
+  if (argc == 1)
+    return 1;
+
+  int x = 1;
+  int i;
+  
+  for(i=1;i<argc;i++){
+    p = &x;
+  }
+
+  return *p; // no-warning
+}
+
+