srcML / srcML

srcML Toolkit
srcml.org
GNU General Public License v3.0
105 stars 26 forks source link

Memory Leak in xpathTransformation Destructor #1990

Open aalramadan opened 4 months ago

aalramadan commented 4 months ago

The destructor of xpathTransformation appears to have a memory leak. The issue is in the conditional check for freeing the compiled_xpath.

Here is the current destructor code:

xpathTransformation::~xpathTransformation() {

    // free the compiled xpath
    if (!compiled_xpath)
        xmlXPathFreeCompExpr(compiled_xpath);

    // free the namespace for any added attributes
    if (attr_ns)
        xmlFreeNs(attr_ns);

    // free the namespace for any added elements
    if (element_ns)
        xmlFreeNs(element_ns);
}

The problem is with this line:

if (!compiled_xpath)
    xmlXPathFreeCompExpr(compiled_xpath);

The if (!compiled_xpath) check means that xmlXPathFreeCompExpr(compiled_xpath) is only called if compiled_xpath is NULL. However, we might want to free compiled_xpath when it is not NULL.

The issue was fixed by changing the line to:

if (compiled_xpath)
    xmlXPathFreeCompExpr(compiled_xpath);