[AST] Fix a null initializer crash for InitListExpr
authorHaojian Wu <hokein.wu@gmail.com>
Fri, 29 May 2020 21:27:05 +0000 (23:27 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 2 Jun 2020 08:48:48 +0000 (10:48 +0200)
Summary:
The Initializer of a InitListExpr can be reset to null, which leads to
nullptr-acces crashes.

Reviewers: sammccall

Subscribers: cfe-commits

Tags: #clang

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

clang/lib/Sema/SemaInit.cpp
clang/test/AST/ast-dump-recovery.cpp

index 56d7ac8..0a98cb2 100644 (file)
@@ -1638,7 +1638,7 @@ void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
 
   expr = Result.getAs<Expr>();
   // FIXME: Why are we updating the syntactic init list?
-  if (!VerifyOnly)
+  if (!VerifyOnly && expr)
     IList->setInit(Index, expr);
 
   if (hadError)
index 9b13f4d..a212ff4 100644 (file)
@@ -181,3 +181,14 @@ void InitializerForAuto() {
 // Verified that the generated call operator is invalid.
 // CHECK: |-CXXMethodDecl {{.*}} invalid operator() 'auto () const -> auto'
 using Escape = decltype([] { return undef(); }());
+
+// CHECK:      VarDecl {{.*}} NoCrashOnInvalidInitList
+// CHECK-NEXT: `-RecoveryExpr {{.*}} '<dependent type>' contains-errors lvalue
+// CHECK-NEXT:   `-InitListExpr
+// CHECK-NEXT:     `-DesignatedInitExpr {{.*}} 'void'
+// CHECK-NEXT:       `-CXXNullPtrLiteralExpr {{.*}} 'nullptr_t'
+struct {
+  int& abc;
+} NoCrashOnInvalidInitList = {
+  .abc = nullptr,
+};