[llvm] 53c08df - [llvm-mt] Use XmlDeleter to free xmlFreeDoc (#128472)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 24 12:54:25 PST 2025


Author: Vitaly Buka
Date: 2025-02-24T12:54:21-08:00
New Revision: 53c08dfc18163ab50a94da344fd93fddac179495

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

LOG: [llvm-mt] Use XmlDeleter to free xmlFreeDoc (#128472)

Fixes memory leak on error in llvm-mt.

Previous https://reviews.llvm.org/D37321 missed this spot.

Added: 
    

Modified: 
    llvm/lib/WindowsManifest/WindowsManifestMerger.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/WindowsManifest/WindowsManifestMerger.cpp b/llvm/lib/WindowsManifest/WindowsManifestMerger.cpp
index a85dcd2ae2859..cdd63686d4711 100644
--- a/llvm/lib/WindowsManifest/WindowsManifestMerger.cpp
+++ b/llvm/lib/WindowsManifest/WindowsManifestMerger.cpp
@@ -32,7 +32,6 @@ void WindowsManifestError::log(raw_ostream &OS) const { OS << Msg; }
 
 class WindowsManifestMerger::WindowsManifestMergerImpl {
 public:
-  ~WindowsManifestMergerImpl();
   Error merge(MemoryBufferRef Manifest);
   std::unique_ptr<MemoryBuffer> getMergedManifest();
 
@@ -45,7 +44,7 @@ class WindowsManifestMerger::WindowsManifestMergerImpl {
     void operator()(xmlDoc *Ptr) { xmlFreeDoc(Ptr); }
   };
   xmlDocPtr CombinedDoc = nullptr;
-  std::vector<xmlDocPtr> MergedDocs;
+  std::vector<std::unique_ptr<xmlDoc, XmlDeleter>> MergedDocs;
   bool Merged = false;
   int BufferSize = 0;
   std::unique_ptr<xmlChar, XmlDeleter> Buffer;
@@ -611,11 +610,6 @@ static void checkAndStripPrefixes(xmlNodePtr Node,
   }
 }
 
-WindowsManifestMerger::WindowsManifestMergerImpl::~WindowsManifestMergerImpl() {
-  for (auto &Doc : MergedDocs)
-    xmlFreeDoc(Doc);
-}
-
 Error WindowsManifestMerger::WindowsManifestMergerImpl::merge(
     MemoryBufferRef Manifest) {
   if (Merged)
@@ -626,17 +620,17 @@ Error WindowsManifestMerger::WindowsManifestMergerImpl::merge(
         "attempted to merge empty manifest");
   xmlSetGenericErrorFunc((void *)this,
                          WindowsManifestMergerImpl::errorCallback);
-  xmlDocPtr ManifestXML = xmlReadMemory(
+  std::unique_ptr<xmlDoc, XmlDeleter> ManifestXML(xmlReadMemory(
       Manifest.getBufferStart(), Manifest.getBufferSize(), "manifest.xml",
-      nullptr, XML_PARSE_NOBLANKS | XML_PARSE_NODICT);
+      nullptr, XML_PARSE_NOBLANKS | XML_PARSE_NODICT));
   xmlSetGenericErrorFunc(nullptr, nullptr);
   if (auto E = getParseError())
     return E;
-  xmlNodePtr AdditionalRoot = xmlDocGetRootElement(ManifestXML);
+  xmlNodePtr AdditionalRoot = xmlDocGetRootElement(ManifestXML.get());
   stripComments(AdditionalRoot);
   setAttributeNamespaces(AdditionalRoot);
   if (CombinedDoc == nullptr) {
-    CombinedDoc = ManifestXML;
+    CombinedDoc = ManifestXML.get();
   } else {
     xmlNodePtr CombinedRoot = xmlDocGetRootElement(CombinedDoc);
     if (!xmlStringsEqual(CombinedRoot->name, AdditionalRoot->name) ||
@@ -648,7 +642,7 @@ Error WindowsManifestMerger::WindowsManifestMergerImpl::merge(
       return E;
     }
   }
-  MergedDocs.push_back(ManifestXML);
+  MergedDocs.push_back(std::move(ManifestXML));
   return Error::success();
 }
 


        


More information about the llvm-commits mailing list