]> granicus.if.org Git - clang/commitdiff
[analyzer] NFC: Forbid array elements of void type.
authorArtem Dergachev <artem.dergachev@gmail.com>
Wed, 17 Jan 2018 22:40:36 +0000 (22:40 +0000)
committerArtem Dergachev <artem.dergachev@gmail.com>
Wed, 17 Jan 2018 22:40:36 +0000 (22:40 +0000)
Represent the symbolic value for results of pointer arithmetic on void pointers
in a different way: instead of making void-typed element regions, make
char-typed element regions.

Add an assertion that ensures that no void-typed regions are ever constructed.

This is a refactoring of internals that should not immediately affect
the analyzer's (default) behavior.

Differential Revision: https://reviews.llvm.org/D40939

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

include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
lib/StaticAnalyzer/Core/ExprEngine.cpp
lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp

index ac214c270492a3dc4cbcff2ac07aca4d682665c2..14909e3160ed06f8fa7cc2e837fe4198cde73391 100644 (file)
@@ -962,7 +962,10 @@ class CXXThisRegion : public TypedValueRegion {
   CXXThisRegion(const PointerType *thisPointerTy,
                 const StackArgumentsSpaceRegion *sReg)
       : TypedValueRegion(sReg, CXXThisRegionKind),
-        ThisPointerTy(thisPointerTy) {}
+        ThisPointerTy(thisPointerTy) {
+    assert(ThisPointerTy->getPointeeType()->getAsCXXRecordDecl() &&
+           "Invalid region type!");
+  }
 
   static void ProfileRegion(llvm::FoldingSetNodeID &ID,
                             const PointerType *PT,
@@ -1076,6 +1079,8 @@ class ElementRegion : public TypedValueRegion {
     assert((!Idx.getAs<nonloc::ConcreteInt>() ||
             Idx.castAs<nonloc::ConcreteInt>().getValue().isSigned()) &&
            "The index must be signed");
+    assert(!elementType.isNull() && !elementType->isVoidType() &&
+           "Invalid region type!");
   }
 
   static void ProfileRegion(llvm::FoldingSetNodeID& ID, QualType elementType,
index c4c6e41da76966dc8c64c322cc8c8da43482e689..7b0964757a9a212c1df59a814420adeec299966a 100644 (file)
@@ -2209,9 +2209,17 @@ void ExprEngine::VisitArraySubscriptExpr(const ArraySubscriptExpr *A,
     ProgramStateRef state = Node->getState();
 
     if (IsGLValueLike) {
-      SVal V = state->getLValue(A->getType(),
-          state->getSVal(Idx, LCtx),
-          state->getSVal(Base, LCtx));
+      QualType T = A->getType();
+
+      // One of the forbidden LValue types! We still need to have sensible
+      // symbolic locations to represent this stuff. Note that arithmetic on
+      // void pointers is a GCC extension.
+      if (T->isVoidType())
+        T = getContext().CharTy;
+
+      SVal V = state->getLValue(T,
+                                state->getSVal(Idx, LCtx),
+                                state->getSVal(Base, LCtx));
       Bldr.generateNode(A, Node, state->BindExpr(A, LCtx, V), nullptr,
           ProgramPoint::PostLValueKind);
     } else if (IsVectorType) {
index 94d29d5a6ba32df0c011fda44f6e40e44c3b35d1..0ec4a81af3e6b546507e402341efdd34d3f6c7b5 100644 (file)
@@ -988,6 +988,12 @@ SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state,
         elementType = resultTy->getPointeeType();
     }
 
+    // Represent arithmetic on void pointers as arithmetic on char pointers.
+    // It is fine when a TypedValueRegion of char value type represents
+    // a void pointer. Note that arithmetic on void pointers is a GCC extension.
+    if (elementType->isVoidType())
+      elementType = getContext().CharTy;
+
     if (Optional<NonLoc> indexV = index.getAs<NonLoc>()) {
       return loc::MemRegionVal(MemMgr.getElementRegion(elementType, *indexV,
                                                        superR, getContext()));