From: Haojian Wu Date: Wed, 6 May 2020 06:56:42 +0000 (+0200) Subject: [clang] Fix a crash on invalid auto. X-Git-Tag: llvmorg-12-init~6856 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c6e1fd70fb24d52daff4ec3fca1a89adfe7c2a0b;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 5f11038..7f3d881 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13208,13 +13208,15 @@ Sema::BuildDeclaratorGroup(MutableArrayRef Group) { DeducedDecl = D; } else if (!Context.hasSameType(DT->getDeducedType(), Deduced)) { auto *AT = dyn_cast(DT); - Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(), - diag::err_auto_different_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_different_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 0000000..f727473 --- /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 +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