C++, libxslt: Freeing the stylesheet document after freeing the style sheet results in crash

StackOverflow https://stackoverflow.com/questions/12342128

  •  01-07-2021
  •  | 
  •  

Question

I'm using libxml2 with libxslt for XML processing from a C++ program. For transforming XML documents with XSL, I use the following function (error handling removed):

xmlDocPtr
transformXmlDocument(
    const xmlDocPtr inputDocument,
    const std::string& stylesheetString
    ) {

    exsltRegisterAll();

    // Read the stylesheet document.
    xmlDocPtr stylesheetDocument = xmlReadMemory(
            stylesheetString.c_str(),
            stylesheetString.length(),
            "stylesheet.xsd",
            0, // No encoding set - get it from the file header.
            0  // No further options.
    );

    // Parse the stylesheet
    xsltStylesheetPtr stylesheet = xsltParseStylesheetDoc(stylesheetDocument);

    // Transform the document
    xmlDocPtr result = xsltApplyStylesheet(stylesheet, inputDocument, 0);

    // Free used resources
    xsltFreeStylesheet(stylesheet);
    xsltCleanupGlobals();

    // Here the program crashes
    xmlFreeDoc(stylesheetDocument);

    return result;
}

The problem is that the program crashes with an access violation (glibc says: free(): invalid pointer: 0x00000000026d8090 *) in the second last line.

I can't find any hints in the docs that xsltFreeStylesheet also frees the underlying document or something, so what is wrong here?

Was it helpful?

Solution

xsltFreeStylesheet also frees the underlying document or something

The fine manual has some hints which would suggest that there's indeed a chance of this scenario happening.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top