[PATCH] [Sema] Don't crash when initializing an incomplete list-initialized variable

David Majnemer david.majnemer at gmail.com
Thu Apr 9 23:46:17 PDT 2015


Hi rsmith,

A list-initialized variable may have an initializer of incomplete type.
We didn't ensure the sub-element type was complete before we
interrogated certain properties of the type.

http://reviews.llvm.org/D8951

Files:
  lib/Sema/SemaExprCXX.cpp
  lib/Sema/SemaInit.cpp
  test/CXX/dcl.decl/dcl.init/dcl.init.list/p3-0x.cpp
  test/Modules/submodules-merge-defs.cpp

Index: lib/Sema/SemaExprCXX.cpp
===================================================================
--- lib/Sema/SemaExprCXX.cpp
+++ lib/Sema/SemaExprCXX.cpp
@@ -5108,6 +5108,13 @@
     return E;
 
   bool IsDecltype = ExprEvalContexts.back().IsDecltype;
+
+  if (!IsDecltype &&
+      RequireCompleteType(E->getExprLoc(), QualType(RT, 0),
+                          diag::err_typecheck_decl_incomplete_type,
+                          SourceRange(E->getLocStart(), E->getLocEnd())))
+      return ExprError();
+
   CXXDestructorDecl *Destructor = IsDecltype ? nullptr : LookupDestructor(RD);
 
   if (Destructor) {
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -1074,6 +1074,17 @@
     // Fall through for subaggregate initialization
   }
 
+  if (SemaRef.RequireCompleteType(expr->getExprLoc(), ElemType, 0)) {
+    if (!VerifyOnly)
+      SemaRef.Diag(expr->getLocStart(),
+                   diag::err_typecheck_decl_incomplete_type)
+        << ElemType << expr->getSourceRange();
+    hadError = true;
+    ++Index;
+    ++StructuredIndex;
+    return;
+  }
+
   // C++ [dcl.init.aggr]p12:
   //
   //   [...] Otherwise, if the member is itself a non-empty
Index: test/CXX/dcl.decl/dcl.init/dcl.init.list/p3-0x.cpp
===================================================================
--- test/CXX/dcl.decl/dcl.init/dcl.init.list/p3-0x.cpp
+++ test/CXX/dcl.decl/dcl.init/dcl.init.list/p3-0x.cpp
@@ -125,3 +125,9 @@
     // expected-note at -2 {{in initialization of temporary of type 'rdar13395022::MoveOnly [1]' created to list-initialize this reference}}
   }
 }
+
+namespace incomplete {
+union U; // expected-note{{forward declaration}}
+U(&&a)[]{};    // expected-error {{variable has incomplete type}}
+U(&&b)[]{123}; // expected-error {{variable has incomplete type}} expected-note {{in initialization of temporary}}
+}
Index: test/Modules/submodules-merge-defs.cpp
===================================================================
--- test/Modules/submodules-merge-defs.cpp
+++ test/Modules/submodules-merge-defs.cpp
@@ -8,7 +8,7 @@
 A pre_a; // expected-error {{must be imported}} expected-error {{must use 'struct'}}
 // expected-note at defs.h:1 +{{here}}
 // FIXME: We should warn that use_a is being used without being imported.
-int pre_use_a = use_a(pre_a); // expected-error {{'A' must be imported}}
+int pre_use_a = use_a(pre_a); // expected-error +{{'A' must be imported}}
 
 B::Inner2 pre_bi; // expected-error +{{must be imported}}
 // expected-note at defs.h:4 +{{here}}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8951.23568.patch
Type: text/x-patch
Size: 2595 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150410/1468e875/attachment.bin>


More information about the cfe-commits mailing list