Skip to content

[clang] Fix use of dangling ptr in CommandLineTest #119798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

macurtis-amd
Copy link
Contributor

If 'GeneratedArgsStorage' ever grows, contained strings may get copied and data pointers stored in 'GeneratedArgs' may become invalid, pointing to deallocated memory.

If 'GeneratedArgsStorage' ever grows, contained strings may get copied and data
pointers stored in 'GeneratedArgs' may become invalid, pointing to deallocated
memory.
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Dec 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 13, 2024

@llvm/pr-subscribers-clang

Author: None (macurtis-amd)

Changes

If 'GeneratedArgsStorage' ever grows, contained strings may get copied and data pointers stored in 'GeneratedArgs' may become invalid, pointing to deallocated memory.


Full diff: https://github.com/llvm/llvm-project/pull/119798.diff

1 Files Affected:

  • (modified) clang/unittests/Frontend/CompilerInvocationTest.cpp (+5-3)
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>

@macurtis-amd macurtis-amd requested a review from ronlieb December 13, 2024 01:22
@macurtis-amd macurtis-amd merged commit 8bf19ec into llvm:main Dec 13, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants