r313569 - [Sema] Fix a pair of crashes when generating exception specifiers with an

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 18 14:28:55 PDT 2017


Author: erichkeane
Date: Mon Sep 18 14:28:55 2017
New Revision: 313569

URL: http://llvm.org/viewvc/llvm-project?rev=313569&view=rev
Log:
[Sema] Fix a pair of crashes when generating exception specifiers with an
error'ed field for a template class' default ctor.

The two examples in the test would both cause a compiler assert when attempting
to calculate the exception specifier for the default constructor for the
template classes. The problem was that dependents of this function expect that
Field->getInClassInitializer (including canThrow) is not nullptr. However, if
the template's initializer has an error, exactly that situation happens.

This patch simply sets the field to be invalid.

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


Added:
    cfe/trunk/test/SemaCXX/init-expr-crash.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=313569&r1=313568&r2=313569&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Sep 18 14:28:55 2017
@@ -12436,7 +12436,8 @@ ExprResult Sema::BuildCXXDefaultInitExpr
       assert(Pattern && "We must have set the Pattern!");
     }
 
-    if (InstantiateInClassInitializer(Loc, Field, Pattern,
+    if (!Pattern->hasInClassInitializer() ||
+        InstantiateInClassInitializer(Loc, Field, Pattern,
                                       getTemplateInstantiationArgs(Field))) {
       // Don't diagnose this again.
       Field->setInvalidDecl();

Added: cfe/trunk/test/SemaCXX/init-expr-crash.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/init-expr-crash.cpp?rev=313569&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/init-expr-crash.cpp (added)
+++ cfe/trunk/test/SemaCXX/init-expr-crash.cpp Mon Sep 18 14:28:55 2017
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c++11
+
+// Test reproduces a pair of crashes that were caused by code attempting
+// to materialize a default constructor's exception specifier.
+
+template <class T> struct A {
+  static T tab[];
+
+  const int M = UNDEFINED; // expected-error {{use of undeclared identifier}}
+
+  int main()
+  {
+    A<char> a;
+
+    return 0;
+  }
+};
+
+template <class T> struct B {
+  static T tab[];
+
+  // expected-error at +1 {{invalid application of 'sizeof' to an incomplete type}}
+  const int N = sizeof(B<char>::tab) / sizeof(char);
+
+  int main()
+  {
+    B<char> b;
+
+    return 0;
+  }
+};




More information about the cfe-commits mailing list