[clang] c6e1fd7 - [clang] Fix a crash on invalid auto.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed May 6 02:52:28 PDT 2020


Author: Haojian Wu
Date: 2020-05-06T11:47:03+02:00
New Revision: c6e1fd70fb24d52daff4ec3fca1a89adfe7c2a0b

URL: https://github.com/llvm/llvm-project/commit/c6e1fd70fb24d52daff4ec3fca1a89adfe7c2a0b
DIFF: https://github.com/llvm/llvm-project/commit/c6e1fd70fb24d52daff4ec3fca1a89adfe7c2a0b.diff

LOG: [clang] Fix a crash on invalid auto.

Summary:
The crash is triggered on accessing a null InitExpr.

For group declaration, e.g. `auto c = a, &d = {a};`, what's happening:

1. each VarDecl is built separately during the parsing stage.
2. perform the semantic analysis (Sema::BuildDeclaratorGroup) to check
whether the type of the two VarDecl is the same, if not mark it as invalid.

in step 1, VarDecl c and d are built, both of them are valid (after D77395),
but d is without the InitExpr attached (under -fno-recovery-ast), crash
happens in step 2 when accessing the source range of d's InitExpr.

Reviewers: sammccall

Subscribers: cfe-commits

Tags: #clang

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

Added: 
    clang/test/SemaCXX/auto-invalid-init-crash.cpp

Modified: 
    clang/lib/Sema/SemaDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5f11038d589e..7f3d8816e78c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13208,13 +13208,15 @@ Sema::BuildDeclaratorGroup(MutableArrayRef<Decl *> Group) {
         DeducedDecl = D;
       } else if (!Context.hasSameType(DT->getDeducedType(), Deduced)) {
         auto *AT = dyn_cast<AutoType>(DT);
-        Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
-             diag::err_auto_
diff erent_deductions)
-          << (AT ? (unsigned)AT->getKeyword() : 3)
-          << Deduced << DeducedDecl->getDeclName()
-          << DT->getDeducedType() << D->getDeclName()
-          << DeducedDecl->getInit()->getSourceRange()
-          << D->getInit()->getSourceRange();
+        auto Dia = Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
+                        diag::err_auto_
diff erent_deductions)
+                   << (AT ? (unsigned)AT->getKeyword() : 3) << Deduced
+                   << DeducedDecl->getDeclName() << DT->getDeducedType()
+                   << D->getDeclName();
+        if (DeducedDecl->hasInit())
+          Dia << DeducedDecl->getInit()->getSourceRange();
+        if (D->getInit())
+          Dia << D->getInit()->getSourceRange();
         D->setInvalidDecl();
         break;
       }

diff  --git a/clang/test/SemaCXX/auto-invalid-init-crash.cpp b/clang/test/SemaCXX/auto-invalid-init-crash.cpp
new file mode 100644
index 000000000000..f727473dd608
--- /dev/null
+++ b/clang/test/SemaCXX/auto-invalid-init-crash.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fno-recovery-ast -verify %s
+
+namespace std {
+template <typename>
+class initializer_list{};
+int a;
+auto c = a, &d = {a}; // expected-error {{'auto' deduced as 'int'}} \
+                         expected-error {{non-const lvalue reference to type}}
+} // namespace std


        


More information about the cfe-commits mailing list