GNOME Bugzilla – Bug 338303
passing a non-XML-Schema document to xmlSchemaParse interferes with xmlFreeDoc
Last modified: 2006-04-13 07:50:26 UTC
Steps to reproduce: 1. Pass a non-XML-Schema document to xmlSchemaParse (like "<test/>") using xmlSchemaNewDocParserCtxt 2. have it fail (which is correct, it's not XML-Schema after all) 2.a. if that's not enough, free the XML document 3. you should get a memory error Stack trace: From Valgrind (trace from lxml, the __pyx_tp_dealloc_* functions basically call xmlFreeNode or xmlFreeDoc): ==20915== Invalid read of size 8 ==20915== at 0x59094F6: __pyx_tp_dealloc_5etree__NodeBase (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x58E2ED6: __pyx_tp_dealloc_5etree__ElementTree (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B6D9E9: (within /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x4B6DC79: PyDict_SetItem (in /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x56CD0C7: ??? ==20915== Address 0x5F6E038 is 0 bytes inside a block of size 120 free'd ==20915== at 0x4A1A5E3: free (in /usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so) ==20915== by 0x5BC39ED: xmlFreeNodeList (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5BC3793: xmlFreeDoc (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C15CA1: (within /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C28D4A: xmlSchemaParse (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x59223CA: __pyx_f_5etree_9XMLSchema___init__ (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B7C67C: (within /usr/lib64/libpython2.4.so.1.0) ==20915== ==20915== Invalid write of size 8 ==20915== at 0x5909502: __pyx_tp_dealloc_5etree__NodeBase (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x58E2ED6: __pyx_tp_dealloc_5etree__ElementTree (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B6D9E9: (within /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x4B6DC79: PyDict_SetItem (in /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x56CD0C7: ??? ==20915== Address 0x5F6E038 is 0 bytes inside a block of size 120 free'd ==20915== at 0x4A1A5E3: free (in /usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so) ==20915== by 0x5BC39ED: xmlFreeNodeList (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5BC3793: xmlFreeDoc (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C15CA1: (within /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C28D4A: xmlSchemaParse (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x59223CA: __pyx_f_5etree_9XMLSchema___init__ (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B7C67C: (within /usr/lib64/libpython2.4.so.1.0) ==20915== ==20915== Invalid read of size 8 ==20915== at 0x58E250F: __pyx_f_5etree_attemptDeallocation (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x5909585: __pyx_tp_dealloc_5etree__NodeBase (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x58E2ED6: __pyx_tp_dealloc_5etree__ElementTree (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B6D9E9: (within /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x4B6DC79: PyDict_SetItem (in /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x56CD0C7: ??? ==20915== Address 0x5F6E060 is 40 bytes inside a block of size 120 free'd ==20915== at 0x4A1A5E3: free (in /usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so) ==20915== by 0x5BC39ED: xmlFreeNodeList (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5BC3793: xmlFreeDoc (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C15CA1: (within /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C28D4A: xmlSchemaParse (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x59223CA: __pyx_f_5etree_9XMLSchema___init__ (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B7C67C: (within /usr/lib64/libpython2.4.so.1.0) ==20915== ==20915== Invalid read of size 4 ==20915== at 0x58E2518: __pyx_f_5etree_attemptDeallocation (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x5909585: __pyx_tp_dealloc_5etree__NodeBase (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x58E2ED6: __pyx_tp_dealloc_5etree__ElementTree (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B6D9E9: (within /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x4B6DC79: PyDict_SetItem (in /usr/lib64/libpython2.4.so.1.0) ==20915== by 0x56CD0C7: ??? ==20915== Address 0x5F6DF30 is 8 bytes inside a block of size 168 free'd ==20915== at 0x4A1A5E3: free (in /usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so) ==20915== by 0x5BC384F: xmlFreeDoc (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C15CA1: (within /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x5C28D4A: xmlSchemaParse (in /usr/lib64/libxml2.so.2.6.23) ==20915== by 0x59223CA: __pyx_f_5etree_9XMLSchema___init__ (in /home/me/source/Python/lxml/lxml-HEAD/src/lxml/etree.so) ==20915== by 0x4B7C67C: (within /usr/lib64/libpython2.4.so.1.0) Other information: Parsing a non-XML-Schema document seems to (partially?) free the XML document itself from within the xmlSchemaParse. This bug does not occur if the document has an XML-Schema root node.
Same thing as 338306, the interface where the document is passed preparsed is not used commonly, as the compilation modifies it. I reproduced the bug, it is fixed in CVS, thanks for the report ! paphio:~/XML -> cat tst.c #include <stdio.h> #include <libxml/parser.h> #include <libxml/tree.h> #include <libxml/xmlschemas.h> int main(int argc, char **argv) { xmlDocPtr schemas; xmlSchemaParserCtxtPtr ctxt; xmlSchemaPtr res; if (argc != 2) return(1); LIBXML_TEST_VERSION schemas = xmlReadFile(argv[1], NULL, 0); if (schemas == NULL) { fprintf(stderr, "Failed to parse %s\n", argv[1]); exit(0); } ctxt = xmlSchemaNewDocParserCtxt(schemas); if (ctxt == NULL) { fprintf(stderr, "Failed to build XSD parser from %s\n", argv[1]); exit(0); } res = xmlSchemaParse(ctxt); if (res != NULL) { fprintf(stderr, "%s is actually an XSD schemas\n", argv[1]); exit(0); } else { fprintf(stderr, "%s is not an XSD schemas, freeing\n", argv[1]); } xmlSchemaFreeParserCtxt(ctxt); xmlFreeDoc(schemas); xmlCleanupParser(); xmlMemoryDump(); return(0); } paphio:~/XML -> valgrind --leak-check=full --leak-resolution=high ./tst test.xml Schemas parser error : The XML document 'test.xml' is not a schema document. test.xml is not an XSD schemas, freeing paphio:~/XML -> Daniel