# https://github.com/itstool/itstool/pull/18
# https://github.com/itstool/itstool/issues/17

From 98d04cdabf1721cb541ecd234c975f13fde4fa41 Mon Sep 17 00:00:00 2001
From: Guido Trentalancia <guido@trentalancia.com>
Date: Wed, 1 Nov 2017 18:20:36 +0100
Subject: [PATCH 1/2] Revert "Be more careful about libxml2 memory management"

This reverts commit 9b84c007a73e8275ca45762f1bfa3ab7c3a852e2.
---
 itstool.in | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

diff --git itstool.in itstool.in
index a16eba9..c1d0585 100755
--- itstool.in
+++ itstool.in
@@ -477,7 +477,6 @@ class Document (object):
         if load_dtd:
             ctxt.loadSubset(1)
         if keep_entities:
-            ctxt.loadSubset(1)
             ctxt.ctxtUseOptions(libxml2.XML_PARSE_DTDLOAD)
             ctxt.replaceEntities(0)
         else:
@@ -1044,7 +1043,6 @@ class Document (object):
         if self._load_dtd:
             ctxt.loadSubset(1)
         if self._keep_entities:
-            ctxt.loadSubset(1)
             ctxt.ctxtUseOptions(libxml2.XML_PARSE_DTDLOAD)
             ctxt.replaceEntities(0)
         else:
@@ -1071,9 +1069,7 @@ class Document (object):
                     ph_node = msg.get_placeholder(child.name).node
                     if self.has_child_elements(ph_node):
                         self.merge_translations(translations, None, ph_node, strict=strict)
-                        newnode = ph_node.copyNode(1)
-                        newnode.setTreeDoc(self._doc)
-                        child.replaceNode(newnode)
+                        child.replaceNode(ph_node)
                     else:
                         repl = self.get_translated(ph_node, translations, strict=strict, lang=lang)
                         child.replaceNode(repl)
@@ -1088,15 +1084,10 @@ class Document (object):
                     (lang + ' ') if lang is not None else '',
                     msgstr.encode('utf-8')))
                 self._xml_err = ''
-                ctxt.doc().freeDoc()
                 return node
         retnode = node.copyNode(2)
-        retnode.setTreeDoc(self._doc)
         for child in xml_child_iter(trnode):
-            newnode = child.copyNode(1)
-            newnode.setTreeDoc(self._doc)
-            retnode.addChild(newnode)
-
+            retnode.addChild(child.copyNode(1))
         ctxt.doc().freeDoc()
         return retnode
 

From 1549b6d12eb2f35e5c7f1b1856c21768e92ba794 Mon Sep 17 00:00:00 2001
From: Guido Trentalancia <guido@trentalancia.com>
Date: Wed, 1 Nov 2017 18:23:44 +0100
Subject: [PATCH 2/2] Fix a segmentation fault bug introduced with version
 2.0.4.

https://github.com/itstool/itstool/issues/17

This fix seems a lot easier than the previous reverted commit.
---
 itstool.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git itstool.in itstool.in
index c1d0585..e492e95 100755
--- itstool.in
+++ itstool.in
@@ -1048,7 +1048,7 @@ class Document (object):
         else:
             ctxt.replaceEntities(1)
         ctxt.parseDocument()
-        trnode = ctxt.doc().getRootElement()
+        trnode = ctxt.doc().getRootElement().copyNode(1)
         try:
             self._check_errors()
         except libxml2.parserError:
