Skip to content

Commit 8a323ad

Browse files
kadircettstellar
authored andcommitted
[clang][SemaTemplate] Fix a stack use after scope
Differential Revision: https://reviews.llvm.org/D120065 (cherry picked from commit c79c13c)
1 parent d61805a commit 8a323ad

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

clang/include/clang/AST/DeclTemplate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,10 +2461,10 @@ class FriendTemplateDecl : public Decl {
24612461
SourceLocation FriendLoc;
24622462

24632463
FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
2464-
MutableArrayRef<TemplateParameterList *> Params,
2464+
TemplateParameterList **Params, unsigned NumParams,
24652465
FriendUnion Friend, SourceLocation FriendLoc)
2466-
: Decl(Decl::FriendTemplate, DC, Loc), NumParams(Params.size()),
2467-
Params(Params.data()), Friend(Friend), FriendLoc(FriendLoc) {}
2466+
: Decl(Decl::FriendTemplate, DC, Loc), NumParams(NumParams),
2467+
Params(Params), Friend(Friend), FriendLoc(FriendLoc) {}
24682468

24692469
FriendTemplateDecl(EmptyShell Empty) : Decl(Decl::FriendTemplate, Empty) {}
24702470

clang/lib/AST/DeclTemplate.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/ADT/FoldingSet.h"
2929
#include "llvm/ADT/None.h"
3030
#include "llvm/ADT/PointerUnion.h"
31+
#include "llvm/ADT/STLExtras.h"
3132
#include "llvm/ADT/SmallVector.h"
3233
#include "llvm/Support/Casting.h"
3334
#include "llvm/Support/ErrorHandling.h"
@@ -1098,7 +1099,13 @@ FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
10981099
SourceLocation L,
10991100
MutableArrayRef<TemplateParameterList *> Params,
11001101
FriendUnion Friend, SourceLocation FLoc) {
1101-
return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
1102+
TemplateParameterList **TPL = nullptr;
1103+
if (!Params.empty()) {
1104+
TPL = new (Context) TemplateParameterList *[Params.size()];
1105+
llvm::copy(Params, TPL);
1106+
}
1107+
return new (Context, DC)
1108+
FriendTemplateDecl(DC, L, TPL, Params.size(), Friend, FLoc);
11021109
}
11031110

11041111
FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,

clang/test/SemaTemplate/friend-template.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,12 @@ namespace rdar12350696 {
329329
foo(b); // expected-note {{in instantiation}}
330330
}
331331
}
332+
333+
namespace StackUseAfterScope {
334+
template <typename T> class Bar {};
335+
class Foo {
336+
// Make sure this doesn't crash.
337+
template <> friend class Bar<int>; // expected-error {{template specialization declaration cannot be a friend}}
338+
bool aux;
339+
};
340+
}

0 commit comments

Comments
 (0)