[PATCH] D55862: [Sema] Don't try to account for the size of an incomplete type in CheckArrayAccess

Bruno Ricci via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 19 01:38:29 PST 2018


riccibruno updated this revision to Diff 178848.
riccibruno marked 4 inline comments as done.

Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55862/new/

https://reviews.llvm.org/D55862

Files:
  lib/Sema/SemaChecking.cpp
  test/SemaCXX/array-bounds.cpp


Index: test/SemaCXX/array-bounds.cpp
===================================================================
--- test/SemaCXX/array-bounds.cpp
+++ test/SemaCXX/array-bounds.cpp
@@ -284,3 +284,12 @@
 int test_struct_multiarray() {
   return multi2[4].arr[0]; // expected-warning {{array index 4 is past the end of the array (which contains 4 elements)}}
 }
+
+namespace PR39746 {
+  struct S;
+  extern S xxx[2];
+  class C {};
+
+  C &f() { return reinterpret_cast<C *>(xxx)[1]; } // no-warning
+  C &g() { return reinterpret_cast<C *>(xxx)[2]; } // no-warning
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -12353,8 +12353,14 @@
       BaseExpr->getType()->getPointeeOrArrayElementType();
   BaseExpr = BaseExpr->IgnoreParenCasts();
   const ConstantArrayType *ArrayTy =
-    Context.getAsConstantArrayType(BaseExpr->getType());
-  if (!ArrayTy)
+      Context.getAsConstantArrayType(BaseExpr->getType());
+  const Type *BaseType = BaseExpr->getType()->getPointeeOrArrayElementType();
+
+  // It is possible that the type of the base expression after IgnoreParenCasts
+  // is incomplete, even though the type of the base expression before
+  // IgnoreParenCasts is complete (see PR39746 for an example). In this case we
+  // have no information about whether the array access is out-of-bounds.
+  if (!ArrayTy || BaseType->isIncompleteType())
     return;
 
   Expr::EvalResult Result;
@@ -12376,7 +12382,6 @@
     if (!size.isStrictlyPositive())
       return;
 
-    const Type *BaseType = BaseExpr->getType()->getPointeeOrArrayElementType();
     if (BaseType != EffectiveType) {
       // Make sure we're comparing apples to apples when comparing index to size
       uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55862.178848.patch
Type: text/x-patch
Size: 1856 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181219/1607d828/attachment.bin>


More information about the cfe-commits mailing list