[clang] 8c5133f - [clang] Fix a null-NSS-access crash in DependentNameType.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 2 05:59:56 PDT 2020


Author: Haojian Wu
Date: 2020-07-02T14:58:32+02:00
New Revision: 8c5133f1855767a16a6045777bed4652bd114d84

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

LOG: [clang] Fix a null-NSS-access crash in DependentNameType.

The DependentNameType must have a non-null NSS. This property could be
violated during typo correction.

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

Added: 
    

Modified: 
    clang/lib/Sema/SemaTemplate.cpp
    clang/test/Parser/cxx-template-decl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 021fcd0f053a..c05ed0b14e3e 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4844,10 +4844,7 @@ bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
     CXXScopeSpec SS;
     DeclarationNameInfo NameInfo;
 
-    if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) {
-      SS.Adopt(ArgExpr->getQualifierLoc());
-      NameInfo = ArgExpr->getNameInfo();
-    } else if (DependentScopeDeclRefExpr *ArgExpr =
+   if (DependentScopeDeclRefExpr *ArgExpr =
                dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) {
       SS.Adopt(ArgExpr->getQualifierLoc());
       NameInfo = ArgExpr->getNameInfo();
@@ -4866,6 +4863,7 @@ bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
       if (Result.getAsSingle<TypeDecl>() ||
           Result.getResultKind() ==
               LookupResult::NotFoundInCurrentInstantiation) {
+        assert(SS.getScopeRep() && "dependent scope expr must has a scope!");
         // Suggest that the user add 'typename' before the NNS.
         SourceLocation Loc = AL.getSourceRange().getBegin();
         Diag(Loc, getLangOpts().MSVCCompat

diff  --git a/clang/test/Parser/cxx-template-decl.cpp b/clang/test/Parser/cxx-template-decl.cpp
index 64e7ca921f57..24cc13cde91f 100644
--- a/clang/test/Parser/cxx-template-decl.cpp
+++ b/clang/test/Parser/cxx-template-decl.cpp
@@ -286,3 +286,17 @@ namespace PR45239 {
   template<int> int b;
   template<int> auto f() -> b<0>; // expected-error +{{}}
 }
+
+namespace NoCrashOnNullNNSTypoCorrection {
+
+int AddObservation(); // expected-note {{declared here}}
+
+template <typename T, typename... Args> // expected-note {{template parameter is declared here}}
+class UsingImpl {};
+class AddObservation {
+  using Using =
+    UsingImpl<AddObservationFn, const int>; // expected-error {{use of undeclared identifier 'AddObservationFn'; did you mean}} \
+                                               expected-error {{template argument for template type parameter must be a type}}
+};
+
+}


        


More information about the cfe-commits mailing list