]> git.sesse.net Git - xml-template/blobdiff - c++0x/xml-template.cpp
Fix an id buffer leak in the C++0x code. simple now passes without leaks.
[xml-template] / c++0x / xml-template.cpp
index a6a7316846db452bdf03e364f9f81a830486aa72..067bf730b6cfa0b2f4af94f64258dd809caee212 100644 (file)
@@ -16,6 +16,7 @@ void clean_node(xmlNode *node)
                xmlNode *frag = xmlNewDocFragment(node->doc);
                xmlReplaceNode(node, frag);
                frag->children = node->children;
+               frag->last = node->last;
        }
 }
 
@@ -27,7 +28,7 @@ Replace::Replace(const string &str)
        : str(str) {}
 
 void Replace::process(xmlNode *node, bool clean) {
-       node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
+       xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
        if (clean) {
                clean_node(node);
        }
@@ -61,7 +62,10 @@ void Clone::process(xmlNode *node, bool clean)
                }
        }
 
-       node->children = NULL;
+       xmlFreeNodeList(node->children);
+       node->content = NULL;
+       node->children = node->last = NULL;
+
        for (auto child : new_nodes) {
                xmlAddChild(node, child);
        }       
@@ -91,20 +95,19 @@ void Substitute::process(xmlNode *node, bool clean)
                if (child->type == XML_ELEMENT_NODE) {
                        // Find the ID, if any.
                        string id;
+                       xmlAttr *id_attr = NULL;
                        for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
                                if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
                                    strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
-                                       id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
-
-                                       if (clean) {
-                                               if (attr->prev == NULL) {
-                                                       child->properties = attr->next;
-                                               } else {
-                                                       attr->prev->next = attr->next;
-                                               }
-                                       }
+                                       xmlChar *id_buf = xmlNodeGetContent(attr->children);
+                                       id = reinterpret_cast<const char *>(id_buf);
+                                       xmlFree(id_buf);
+                                       id_attr = attr;
                                }
                        }
+                       if (clean && id_attr != NULL) {
+                               xmlRemoveProp(id_attr);
+                       }
 
                        // Check all substitutions to see if we found anything appropriate.
                        for (auto it : substitution_map) {