[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
Tue Dec 18 15:43:24 PST 2018


riccibruno created this revision.
riccibruno added reviewers: bkramer, efriedma.
riccibruno added a project: clang.
Herald added a subscriber: cfe-commits.

It is possible that the base type in CheckArrayAccess is incomplete even
though the effective type is complete. In this case don't try to account
for the size of the base type. This fixes PR39746.


Repository:
  rC Clang

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]; // expected-note {{array 'xxx' declared here}}
+  class C {};
+
+  C &f() { return reinterpret_cast<C *>(xxx)[1]; } // no-warning
+  C &g() { return reinterpret_cast<C *>(xxx)[2]; } // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -12377,7 +12377,10 @@
       return;
 
     const Type *BaseType = BaseExpr->getType()->getPointeeOrArrayElementType();
-    if (BaseType != EffectiveType) {
+    // It is possible that the base type is incomplete (see PR39746), even
+    // though the effective type is complete. In this case we have no info
+    // about the size of the base type and so skip the following adjustment.
+    if ((BaseType != EffectiveType) && !BaseType->isIncompleteType(nullptr)) {
       // Make sure we're comparing apples to apples when comparing index to size
       uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
       uint64_t array_typesize = Context.getTypeSize(BaseType);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55862.178807.patch
Type: text/x-patch
Size: 1538 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181218/b8072606/attachment-0001.bin>


More information about the cfe-commits mailing list