From 93561cd605d66dbaf370041c11448f3e4c3f929b Mon Sep 17 00:00:00 2001 From: Eric Beckmann Date: Wed, 26 Jul 2017 18:33:21 +0000 Subject: [PATCH] Unlink nodes instead of copying, to avoid memory problems. llvm-svn: 309151 --- lib/WindowsManifest/WindowsManifestMerger.cpp | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/WindowsManifest/WindowsManifestMerger.cpp b/lib/WindowsManifest/WindowsManifestMerger.cpp index 0c9e7fda579..ce68f4db551 100644 --- a/lib/WindowsManifest/WindowsManifestMerger.cpp +++ b/lib/WindowsManifest/WindowsManifestMerger.cpp @@ -93,21 +93,18 @@ Error mergeAttributes(XMLNodeImpl OriginalNode, XMLNodeImpl AdditionalNode) { Error treeMerge(XMLNodeImpl OriginalRoot, XMLNodeImpl AdditionalRoot) { #if LLVM_LIBXML2_ENABLED XMLNodeImpl AdditionalFirstChild = AdditionalRoot->children; + xmlNode StoreNext; for (XMLNodeImpl Child = AdditionalFirstChild; Child; Child = Child->next) { XMLNodeImpl OriginalChildWithName; if (!isMergeableElement(Child->name) || !(OriginalChildWithName = getChildWithName(OriginalRoot, Child->name))) { - XMLNodeImpl NewChild = xmlCopyNode(Child, 1); - if (!NewChild) - return make_error(Twine("error when copying ") + - FROM_XML_CHAR(Child->name)); - if (NewChild->ns) - xmlFreeNs(NewChild->ns); // xmlCopyNode explicitly defines default - // namespace, undo this here. - if (!xmlAddChild(OriginalRoot, NewChild)) + StoreNext.next = Child->next; + xmlUnlinkNode(Child); + if (!xmlAddChild(OriginalRoot, Child)) return make_error(Twine("could not merge ") + - FROM_XML_CHAR(NewChild->name)); + FROM_XML_CHAR(Child->name)); + Child = &StoreNext; } else if (auto E = treeMerge(OriginalChildWithName, Child)) { return E; } @@ -167,11 +164,7 @@ Error WindowsManifestMerger::merge(const MemoryBuffer &Manifest) { return E; } } else { - XMLNodeImpl NewChild = xmlCopyNode(AdditionalRoot, 1); - if (!NewChild) - return make_error("could not copy manifest"); - if (!xmlAddChild(CombinedRoot, NewChild)) - return make_error("could not append manifest"); + return make_error("multiple root nodes"); } } MergedDocs.push_back(ManifestXML);