From ed13d8c66781b50ff007cb089c5905f9bb9e8af2 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Fri, 18 Dec 2020 13:25:18 -0800 Subject: [PATCH] Fix memory leak complicated non-type template arguments. --- clang/include/clang/AST/ASTContext.h | 4 ++-- clang/lib/AST/TemplateBase.cpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 0c5d82b3e9aa..a9bfdb4d5fa5 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -2818,8 +2818,8 @@ public: /// for destruction. template void addDestruction(T *Ptr) const { if (!std::is_trivially_destructible::value) { - auto DestroyPtr = [](void *V) { static_cast(V)->~T(); }; - AddDeallocation(DestroyPtr, Ptr); + auto DestroyPtr = [](void *V) { ((T*)V)->~T(); }; + AddDeallocation(DestroyPtr, (void*)Ptr); } } diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp index 0029c90a0ab6..a746db315d85 100644 --- a/clang/lib/AST/TemplateBase.cpp +++ b/clang/lib/AST/TemplateBase.cpp @@ -137,6 +137,7 @@ TemplateArgument::TemplateArgument(const ASTContext &Ctx, QualType Type, else { Value.Kind = UncommonValue; Value.Value = new (Ctx) APValue(V); + Ctx.addDestruction(Value.Value); Value.Type = Type.getAsOpaquePtr(); } }