Skip to content

Fix segmentation fault on object free when using extension#11

Open
eric-hemasystems wants to merge 1 commit intoglejeune:masterfrom
eric-hemasystems:segfault-stylesheet-ext
Open

Fix segmentation fault on object free when using extension#11
eric-hemasystems wants to merge 1 commit intoglejeune:masterfrom
eric-hemasystems:segfault-stylesheet-ext

Conversation

@eric-hemasystems
Copy link
Copy Markdown

If a XSL file activates an extension. For example:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://exslt.org/functions" extension-element-prefixes="func">
</xsl:stylesheet>

Then we load two instances of the library both referencing that XSL file:

require 'xml/xslt'

xslt1 = XML::XSLT.new()
xslt1.xsl = 'xslt.xsl'

xslt2 = XML::XSLT.new()
xslt2.xsl = 'xslt.xsl'

Then we get a segmentation fault. I believe this is because of the:

xsltCleanupGlobals();
xmlCleanupParser();
xmlMemoryDump();

that is run whenever a XML::XSLT object is garbage collected. While the XSLT tutorial does suggest these methods, if you look at the documentation to xmlCleanupParser you will see it is clear this should only be done when the library is no longer needed:

Since our other instance may still be using the library it now gets an error when it tries to cleanup it's stylesheet that is using an extension.

This commit removes that freeing of memory as we don't really have any idea when the calling application might be done with the library. This is also per the recommendation in the documentation where it says:

It's sometimes very hard to guess if libxml2 is in use in the
application, some libraries or plugins may use it without notice. In
case of doubt abstain from calling this function...

If a XSL file activates an extension. For example:

    <?xml version='1.0' encoding='UTF-8'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://exslt.org/functions" extension-element-prefixes="func">
    </xsl:stylesheet>

Then we load two instances of the library both referencing that XSL
file:

    require 'xml/xslt'

    xslt1 = XML::XSLT.new()
    xslt1.xsl = 'xslt.xsl'

    xslt2 = XML::XSLT.new()
    xslt2.xsl = 'xslt.xsl'

Then we get a segmentation fault. I believe this is because of the:

    xsltCleanupGlobals();
    xmlCleanupParser();
    xmlMemoryDump();

that is run whenever a XML::XSLT object is garbage collected. While
the XSLT tutorial does suggest these methods be called:

http://xmlsoft.org/XSLT/tutorial/libxslttutorial.html#cleanup

If you look at the documentation to `xmlCleanupParser` you will see
it is clear this should only be done when the library is no longer
needed:

http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser

Since our other instance may still be using the library it now gets an
error when it tries to cleanup it's stylesheet that is using an
extension.

This commit removes that freeing of memory as we don't really have any
idea when the calling application might be done with the library. This
is also per the recommendation in the documentation where it says:

    It's sometimes very hard to guess if libxml2 is in use in the
    application, some libraries or plugins may use it without notice. In
    case of doubt abstain from calling this function...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant