Skip to content

[lldb][test] Free buffers in demangling tests to avoid leaks #142676

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
Jun 4, 2025

Conversation

rupprecht
Copy link
Collaborator

Test case added by f669b9c / #137793. Note that the DemanglingParts case above also frees the buffer; this new test case is inconsistent.

@rupprecht rupprecht requested a review from Michael137 June 3, 2025 21:58
@rupprecht rupprecht requested a review from JDevlieghere as a code owner June 3, 2025 21:58
@llvmbot llvmbot added the lldb label Jun 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

@llvm/pr-subscribers-lldb

Author: Jordan Rupprecht (rupprecht)

Changes

Test case added by f669b9c / #137793. Note that the DemanglingParts case above also frees the buffer; this new test case is inconsistent.


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

1 Files Affected:

  • (modified) lldb/unittests/Core/MangledTest.cpp (+4-1)
diff --git a/lldb/unittests/Core/MangledTest.cpp b/lldb/unittests/Core/MangledTest.cpp
index dfdc026521379..e9dfd05459642 100644
--- a/lldb/unittests/Core/MangledTest.cpp
+++ b/lldb/unittests/Core/MangledTest.cpp
@@ -642,8 +642,10 @@ TEST_P(DemanglingInfoCorrectnessTestFixutre, Correctness) {
   // function names.
   if (Root->getKind() !=
           llvm::itanium_demangle::Node::Kind::KFunctionEncoding &&
-      Root->getKind() != llvm::itanium_demangle::Node::Kind::KDotSuffix)
+      Root->getKind() != llvm::itanium_demangle::Node::Kind::KDotSuffix) {
+    std::free(OB.getBuffer());
     return;
+  }
 
   ASSERT_TRUE(OB.NameInfo.hasBasename());
 
@@ -670,6 +672,7 @@ TEST_P(DemanglingInfoCorrectnessTestFixutre, Correctness) {
                        return_right, qualifiers, suffix);
 
   EXPECT_EQ(reconstructed_name, demangled);
+  std::free(OB.getBuffer());
 }
 
 INSTANTIATE_TEST_SUITE_P(

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Maybe an opportunity to use a unique_ptr with a custom deleter? Something like:

struct TrackingOutputBufferDeleter {
  void operator()(TrackingOutputBuffer* TOB) {
    if (!TOB)
      return;
    std::free(TOB->getBuffer());
    std::free(TOB);
  }
};

Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! i keep forgetting

@rupprecht
Copy link
Collaborator Author

TrackingOutputBufferDeleter

Yes, that would be a good improvement, especially since the test method here has an early return.

I'd like to land this as-is to plug the leak, but I'll start a new branch now w/ that suggestion.

@rupprecht rupprecht merged commit 79f298e into llvm:main Jun 4, 2025
12 checks passed
@rupprecht
Copy link
Collaborator Author

LGTM. Maybe an opportunity to use a unique_ptr with a custom deleter? Something like:

struct TrackingOutputBufferDeleter {
  void operator()(TrackingOutputBuffer* TOB) {
    if (!TOB)
      return;
    std::free(TOB->getBuffer());
    std::free(TOB);
  }
};

Sent #142815. The suggestion is almost perfect. The buffer can be free'd, but TOB must be deleted, otherwise there's a mismatch: alloc-dealloc-mismatch (operator new vs free).

rupprecht added a commit that referenced this pull request Jun 5, 2025
…r deleter (#142815)

Suggested in #142676 as a way to automatically free the buffer in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants