[Lldb-commits] [lldb] eb5e11f - [lldb] Fix RichManglingContext::FromCxxMethodName() leak

Jordan Rupprecht via lldb-commits lldb-commits at lists.llvm.org
Wed Apr 21 12:34:48 PDT 2021


Author: Jordan Rupprecht
Date: 2021-04-21T12:32:08-07:00
New Revision: eb5e11f460b1bcace77127de00b8bd15d409ea3a

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

LOG: [lldb] Fix RichManglingContext::FromCxxMethodName() leak

`RichManglingContext::FromCxxMethodName` allocates a m_cxx_method_parser, but never deletes it.

This fixes a `-DLLVM_USE_SANITIZER=Leaks` failure.

Reviewed By: teemperor

Differential Revision: https://reviews.llvm.org/D100795

Added: 
    

Modified: 
    lldb/include/lldb/Core/RichManglingContext.h
    lldb/source/Core/RichManglingContext.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/RichManglingContext.h b/lldb/include/lldb/Core/RichManglingContext.h
index 68f80e73b7241..61616d9ac27a4 100644
--- a/lldb/include/lldb/Core/RichManglingContext.h
+++ b/lldb/include/lldb/Core/RichManglingContext.h
@@ -29,7 +29,7 @@ class RichManglingContext {
     m_ipd_buf[0] = '\0';
   }
 
-  ~RichManglingContext() { std::free(m_ipd_buf); }
+  ~RichManglingContext();
 
   /// Use the ItaniumPartialDemangler to obtain rich mangling information from
   /// the given mangled name.
@@ -77,6 +77,9 @@ class RichManglingContext {
 
   /// Members for ItaniumPartialDemangler
   llvm::ItaniumPartialDemangler m_ipd;
+  /// Note: m_ipd_buf is a raw pointer due to being resized by realloc via
+  /// ItaniumPartialDemangler. It should be managed with malloc/free, not
+  /// new/delete.
   char *m_ipd_buf;
   size_t m_ipd_buf_size;
 
@@ -86,6 +89,9 @@ class RichManglingContext {
   /// dependency. Instead keep a llvm::Any and cast it on-access in the cpp.
   llvm::Any m_cxx_method_parser;
 
+  /// Clean up memory when using PluginCxxLanguage
+  void ResetCxxMethodParser();
+
   /// Clean up memory and set a new info provider for this instance.
   void ResetProvider(InfoProvider new_provider);
 

diff  --git a/lldb/source/Core/RichManglingContext.cpp b/lldb/source/Core/RichManglingContext.cpp
index 2094d96acd7c0..2dcb1407e6c74 100644
--- a/lldb/source/Core/RichManglingContext.cpp
+++ b/lldb/source/Core/RichManglingContext.cpp
@@ -19,7 +19,12 @@ using namespace lldb;
 using namespace lldb_private;
 
 // RichManglingContext
-void RichManglingContext::ResetProvider(InfoProvider new_provider) {
+RichManglingContext::~RichManglingContext() {
+  std::free(m_ipd_buf);
+  ResetCxxMethodParser();
+}
+
+void RichManglingContext::ResetCxxMethodParser() {
   // If we want to support parsers for other languages some day, we need a
   // switch here to delete the correct parser type.
   if (m_cxx_method_parser.hasValue()) {
@@ -27,6 +32,10 @@ void RichManglingContext::ResetProvider(InfoProvider new_provider) {
     delete get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser);
     m_cxx_method_parser.reset();
   }
+}
+
+void RichManglingContext::ResetProvider(InfoProvider new_provider) {
+  ResetCxxMethodParser();
 
   assert(new_provider != None && "Only reset to a valid provider");
   m_provider = new_provider;


        


More information about the lldb-commits mailing list