[clang] 8bf19ec - [clang] Fix use of dangling ptr in CommandLineTest (#119798)

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 13 03:27:03 PST 2024


Author: macurtis-amd
Date: 2024-12-13T05:27:00-06:00
New Revision: 8bf19ec444593b3076a446a8eeb5042bbf79dc65

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

LOG: [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.

Added: 
    

Modified: 
    clang/unittests/Frontend/CompilerInvocationTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp
index 4ff6824f1e21e3..94ab9fe8451e0a 100644
--- a/clang/unittests/Frontend/CompilerInvocationTest.cpp
+++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp
@@ -31,17 +31,19 @@ class CommandLineTest : public ::testing::Test {
 public:
   IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
   SmallVector<const char *, 32> GeneratedArgs;
-  SmallVector<std::string, 32> 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 <typename M>


        


More information about the cfe-commits mailing list