Re: [xml] xmlDOMWrapCloneNode problem


My bad. I did not notice how many left overs I have from my
environment until I moved the code in a complete isolation - something
I should have done at first place. Sorry about that.

The following is a piece of code that depends only on libxml2 (tested
with v2.7.8) and STL (I hope this is not a problem).

The symptom of the problem is that the dump is accessing memory that
is already freed. Please make sure you are compiling the code with the
necessary flags (in debug)  to make sure the problem is 100%


#include "libxml/parser.h"
#include "libxml/tree.h"

#include <string>

static void dump(const xmlDocPtr pDoc, std::string& xmlContent)
    xmlChar* outBuf;
    int      outSize;
    xmlDocDumpFormatMemory(pDoc, &outBuf, &outSize, TRUE);
    xmlContent = std::string((const char *) outBuf);

int main(int argc, char **argv)
    std::string str =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
"<w:settings xmlns:m=\"\"";
" xmlns:w=\"\";>"
        "<m:mathFont m:val=\"Cambria Math\"/>"

    xmlParserCtxtPtr context = xmlNewParserCtxt();
    if (context == NULL)
        return 1;

    xmlDocPtr pDoc = xmlCtxtReadMemory(context, str.c_str(),
(int)str.size(), "dummy1.xml", NULL, 0);


    xmlNodePtr root1 = xmlDocGetRootElement(pDoc);

        xmlDocPtr doc2 = xmlNewDoc(BAD_CAST "1.0");
        doc2->standalone = TRUE;
        xmlNodePtr root = xmlNewDocNode(doc2, NULL, BAD_CAST "settings", NULL);
        if (root == NULL)
            return 1;

        xmlDocSetRootElement(doc2, root);

        xmlNsPtr ns = xmlNewNs(root, BAD_CAST
BAD_CAST "w");
        if (ns == NULL)
            return 1;

        xmlSetNs(root, ns);

        xmlNsPtr mns = xmlNewNs(root, BAD_CAST
        if (mns == NULL)
            return 1;

        xmlNodePtr mathPr = xmlNewDocNode(doc2, mns, BAD_CAST"mathPr", NULL);

        xmlNodePtr mathFont = root1->children->children;

        xmlNodePtr newChildNode;
        xmlDOMWrapCloneNode(NULL,                // xmlDOMWrapCtxtPtr - optional
                            mathFont->doc,       // sourceDoc (must not be NULL)
                            mathFont,            // node to clone
                            &newChildNode,       // destNode
                            mathPr->doc,         // destDoc
                            mathPr,              // destParent
                            TRUE,                // deep = clone children
                            0                    // options (ATTRIBUTE_UNUSED)

        xmlAddChild(mathPr, newChildNode);

        xmlAddChild(root, mathPr);

        std::string outStr;
        dump(doc2, outStr);

    return 0;

On Mon, Aug 13, 2012 at 6:53 PM, Noam Postavsky
<npostavs users sourceforge net> wrote:
On Sat, Aug 11, 2012 at 3:35 PM, George Georgiev
<george georgiev sf gmail com> wrote:

I am new to this group. I reported this issue a while ago and I have
no response so far.

Could you please advice, do I need to report it in a different manner
or what?

Generally, it helps if you can post a minimal but complete program
that somebody else could just compile and run.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]