From 8bf19ec444593b3076a446a8eeb5042bbf79dc65 Mon Sep 17 00:00:00 2001 From: macurtis-amd Date: Fri, 13 Dec 2024 05:27:00 -0600 Subject: [clang] Fix use of dangling ptr in CommandLineTest (#119798) If 'GeneratedArgsStorage' ever grows, contained strings may get copied and data pointers stored in 'GeneratedArgs' may become invalid, pointing to deallocated memory. --- clang/unittests/Frontend/CompilerInvocationTest.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'clang/unittests/Frontend/CompilerInvocationTest.cpp') diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp index 4ff6824..94ab9fe 100644 --- a/clang/unittests/Frontend/CompilerInvocationTest.cpp +++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp @@ -31,17 +31,19 @@ class CommandLineTest : public ::testing::Test { public: IntrusiveRefCntPtr Diags; SmallVector GeneratedArgs; - SmallVector GeneratedArgsStorage; + BumpPtrAllocator Alloc; + StringSaver StringPool; CompilerInvocation Invocation; const char *operator()(const Twine &Arg) { - return GeneratedArgsStorage.emplace_back(Arg.str()).c_str(); + return StringPool.save(Arg).data(); } CommandLineTest() : Diags(CompilerInstance::createDiagnostics( *llvm::vfs::getRealFileSystem(), new DiagnosticOptions(), - new TextDiagnosticBuffer())) {} + new TextDiagnosticBuffer())), + StringPool(Alloc) {} }; template -- cgit v1.1