]> granicus.if.org Git - clang/commitdiff
[analyzer] dynamic_cast: Better model cast from a reference.
authorAnna Zaks <ganna@apple.com>
Tue, 10 Apr 2012 21:29:03 +0000 (21:29 +0000)
committerAnna Zaks <ganna@apple.com>
Tue, 10 Apr 2012 21:29:03 +0000 (21:29 +0000)
Generate a sink when the dynamic_cast from a reference fails to
represent a thrown exception.

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

lib/StaticAnalyzer/Core/ExprEngineC.cpp
lib/StaticAnalyzer/Core/RegionStore.cpp
test/Analysis/dynamic-cast.cpp

index 5070e9d111825d4be5dc32fbd210a8400a858add..57efd463aec232d64c81bae7035dad7c3ecf0890 100644 (file)
@@ -304,14 +304,21 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
           val = getStoreManager().evalDynamicCast(val, T, Failed);
 
         if (Failed) {
-          // If the cast fails, conjure symbol constrained to 0.
-          DefinedOrUnknownSVal NewSym = svalBuilder.getConjuredSymbolVal(NULL,
-                                 CastE, LCtx, resultType,
-                                 currentBuilderContext->getCurrentBlockCount());
-          DefinedOrUnknownSVal Constraint = svalBuilder.evalEQ(state,
-                                 NewSym, svalBuilder.makeZeroVal(resultType));
-          state = state->assume(Constraint, true);
-          state = state->BindExpr(CastE, LCtx, NewSym);
+          if (T->isReferenceType()) {
+            // A bad_cast exception is thrown if input value is a reference.
+            // Currently, we model this, by generating a sink.
+            Bldr.generateNode(CastE, Pred, state, true);
+            continue;
+          } else {
+            // If the cast fails on a pointer, conjure symbol constrained to 0.
+            DefinedOrUnknownSVal NewSym = svalBuilder.getConjuredSymbolVal(NULL,
+                CastE, LCtx, resultType,
+                currentBuilderContext->getCurrentBlockCount());
+            DefinedOrUnknownSVal Constraint = svalBuilder.evalEQ(state,
+                NewSym, svalBuilder.makeZeroVal(resultType));
+            state = state->assume(Constraint, true);
+            state = state->BindExpr(CastE, LCtx, NewSym);
+          }
         } else {
           // If we don't know if the cast succeeded, conjure a new symbol.
           if (val.isUnknown()) {
index 8fb7e88b38b3da42be9a4ddc1652cbf3192a0ae8..cc3ea8c3bf548f48242ba1354f8a58e069433e36 100644 (file)
@@ -896,11 +896,7 @@ SVal RegionStoreManager::evalDynamicCast(SVal base, QualType derivedType,
     return UnknownVal();
   const MemRegion *BaseRegion = baseRegVal->stripCasts();
 
-  // Assume the derived class is a pointer to a CXX record.
-  // TODO: Note, we do not model reference types: a bad_cast exception is thrown
-  // when a cast of reference fails, but we just return an UnknownVal.
-  if (!derivedType->isPointerType())
-    return UnknownVal();
+  // Assume the derived class is a pointer or a reference to a CXX record.
   derivedType = derivedType->getPointeeType();
   assert(!derivedType.isNull());
   const CXXRecordDecl *DerivedDecl = derivedType->getAsCXXRecordDecl();
index 0d0c80fc120ec29ffe58758e75985f438699b0bb..8d1fde8cc5c1563cfe585cc00beac79b22349c5e 100644 (file)
@@ -167,10 +167,18 @@ int testCastToVoidStar() {
   return *res; // no warning
 }
 
-int testReference() {
+int testReferenceSuccesfulCast() {
+  B rb;
+  B &b = dynamic_cast<B&>(rb);
+  int *x = 0;
+  return *x; // expected-warning {{Dereference of null pointer}}
+}
+
+int testReferenceFailedCast() {
   A a;
   B &b = dynamic_cast<B&>(a);
-  return b.m; // no warning
+  int *x = 0;
+  return *x; // no warning (An exception is thrown by the cast.)
 }
 
 // False negatives.