Python crash on comment validation
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Fix Released
|
High
|
scoder |
Bug Description
If you attempt to validate Comment instance (for example when iterating through tree parts), entire Python process crashes.
Tested on:
Python : (2, 6, 6, 'final', 0)
lxml.etree : (3, 2, 3, 0)
libxml used : (2, 9, 0)
libxml compiled : (2, 9, 0)
libxslt used : (1, 1, 28)
libxslt compiled : (1, 1, 28)
And can be replicated with:
from lxml import etree
schemaString = """<xs:schema xmlns:xs="http://
<xs:element name="root">
</xs:element>
</xs:schema>"""
treeString = """<root>
<element1/>
<element2/>
</root>"""
schemaTree = etree.fromstrin
schema = etree.XMLSchema
tree = etree.fromstrin
for part in tree:
print "Validating part '%s'.." % part
isPartValid = schema.
print "Part valid - %s." % isPartValid
Last output message that can be seen is "Validating part '<!--comment-
Note that the XSD in this example is not quite compatible with validating the tree part by part (as it would instead make sense to validate it by passing the whole tree), but this is just simplified example that can reproduce the problem.
Thanks for the report and the excellent test case. Here is a fix:
diff -r 74c41e0ab944 src/lxml/ apihelpers. pxi apihelpers. pxi Fri Nov 15 16:15:42 2013 +0100 apihelpers. pxi Thu Nov 28 18:08:22 2013 +0100
python. _fqtypename( input) ELEMENT_ NODE:
python. _fqtypename( input) idNode( node)
--- a/src/lxml/
+++ b/src/lxml/
@@ -58,7 +58,7 @@
else:
raise TypeError, u"Invalid input object: %s" % \
- if node is None:
+ if node is None or node._c_node.type != tree.XML_
raise ValueError, u"Input object has no element: %s" % \
_assertVal