[llvm-branch-commits] [clang] 8a323ad - [clang][SemaTemplate] Fix a stack use after scope

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Feb 28 10:54:46 PST 2022


Author: Kadir Cetinkaya
Date: 2022-02-28T10:49:35-08:00
New Revision: 8a323ada234ba2859942772b446bf2692dadb573

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

LOG: [clang][SemaTemplate] Fix a stack use after scope

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

(cherry picked from commit c79c13cae61564d3a03831013ea24ff483ee3e82)

Added: 
    

Modified: 
    clang/include/clang/AST/DeclTemplate.h
    clang/lib/AST/DeclTemplate.cpp
    clang/test/SemaTemplate/friend-template.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h
index d216b359816e8..319e605a8a1c5 100644
--- a/clang/include/clang/AST/DeclTemplate.h
+++ b/clang/include/clang/AST/DeclTemplate.h
@@ -2461,10 +2461,10 @@ class FriendTemplateDecl : public Decl {
   SourceLocation FriendLoc;
 
   FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
-                     MutableArrayRef<TemplateParameterList *> Params,
+                     TemplateParameterList **Params, unsigned NumParams,
                      FriendUnion Friend, SourceLocation FriendLoc)
-      : Decl(Decl::FriendTemplate, DC, Loc), NumParams(Params.size()),
-        Params(Params.data()), Friend(Friend), FriendLoc(FriendLoc) {}
+      : Decl(Decl::FriendTemplate, DC, Loc), NumParams(NumParams),
+        Params(Params), Friend(Friend), FriendLoc(FriendLoc) {}
 
   FriendTemplateDecl(EmptyShell Empty) : Decl(Decl::FriendTemplate, Empty) {}
 

diff  --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp
index 223f06b9db1c9..d9ff3517a589c 100644
--- a/clang/lib/AST/DeclTemplate.cpp
+++ b/clang/lib/AST/DeclTemplate.cpp
@@ -28,6 +28,7 @@
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/None.h"
 #include "llvm/ADT/PointerUnion.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -1098,7 +1099,13 @@ FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
                            SourceLocation L,
                            MutableArrayRef<TemplateParameterList *> Params,
                            FriendUnion Friend, SourceLocation FLoc) {
-  return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
+  TemplateParameterList **TPL = nullptr;
+  if (!Params.empty()) {
+    TPL = new (Context) TemplateParameterList *[Params.size()];
+    llvm::copy(Params, TPL);
+  }
+  return new (Context, DC)
+      FriendTemplateDecl(DC, L, TPL, Params.size(), Friend, FLoc);
 }
 
 FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,

diff  --git a/clang/test/SemaTemplate/friend-template.cpp b/clang/test/SemaTemplate/friend-template.cpp
index e9b2b9b8e64e5..2dcee6c76da7d 100644
--- a/clang/test/SemaTemplate/friend-template.cpp
+++ b/clang/test/SemaTemplate/friend-template.cpp
@@ -329,3 +329,12 @@ namespace rdar12350696 {
     foo(b); // expected-note {{in instantiation}}
   }
 }
+
+namespace StackUseAfterScope {
+template <typename T> class Bar {};
+class Foo {
+  // Make sure this doesn't crash.
+  template <> friend class Bar<int>; // expected-error {{template specialization declaration cannot be a friend}}
+  bool aux;
+};
+}


        


More information about the llvm-branch-commits mailing list