From 26ea670d9723ae116fc273a01a0eca38c41c7581 Mon Sep 17 00:00:00 2001 From: Steven Perron Date: Thu, 19 Sep 2024 15:10:56 -0400 Subject: [PATCH] Fix mismatched memory allocation and deallocation. (#6923) In NestedNameSpecifier.cpp, the Append function allocates data using `new`: https://github.com/microsoft/DirectXShaderCompiler/blob/e1bb926f63ba44119577c845e9d7a1a10c0f50e9/tools/clang/lib/AST/NestedNameSpecifier.cpp#L449-L450 However, if it deallocated using `free`: https://github.com/microsoft/DirectXShaderCompiler/blob/e1bb926f63ba44119577c845e9d7a1a10c0f50e9/tools/clang/lib/AST/NestedNameSpecifier.cpp#L514-L516 This can cause problems. I'm changing the `free` to `delete`. --- tools/clang/lib/AST/NestedNameSpecifier.cpp | 4 ++-- .../test/SemaHLSL/nestednamespecifier.hlsl | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tools/clang/test/SemaHLSL/nestednamespecifier.hlsl diff --git a/tools/clang/lib/AST/NestedNameSpecifier.cpp b/tools/clang/lib/AST/NestedNameSpecifier.cpp index d449018cb..492dd6bc0 100644 --- a/tools/clang/lib/AST/NestedNameSpecifier.cpp +++ b/tools/clang/lib/AST/NestedNameSpecifier.cpp @@ -512,7 +512,7 @@ operator=(const NestedNameSpecifierLocBuilder &Other) { // Free our storage, if we have any. if (BufferCapacity) { - free(Buffer); + delete[] Buffer; // HLSL Change: Use overridable operator delete BufferCapacity = 0; } @@ -647,7 +647,7 @@ void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context, void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) { if (BufferCapacity) - free(Buffer); + delete[] Buffer; // HLSL Change: Use overridable operator delete if (!Other) { Representation = nullptr; diff --git a/tools/clang/test/SemaHLSL/nestednamespecifier.hlsl b/tools/clang/test/SemaHLSL/nestednamespecifier.hlsl new file mode 100644 index 000000000..f6d539e59 --- /dev/null +++ b/tools/clang/test/SemaHLSL/nestednamespecifier.hlsl @@ -0,0 +1,17 @@ +// RUN: %dxc -T cs_6_0 -E main -HV 2021 -verify %s + +// expected-no-diagnostics + +template +class C { + C cast(); +}; + +template +C +C::cast() { + C result; + return result; +} + +[numthreads(64, 1, 1)] void main() {}