Permalink
Browse files

Fix a segfault when replacing an unparented node.

The libxml2 function xmlReplaceNode will return NULL when the node to be
replaced has no parent.

Nokogiri now anticipates this and returns an exception instead of segfaulting.

This fixes issue #759, which includes a good script to reproduce and which
was reported by etm.
  • Loading branch information...
1 parent 7aefe8a commit 5a09ab9e75cedb2d934bb4101dda3f7e7374fb6b @ender672 ender672 committed Sep 12, 2012
Showing with 10 additions and 1 deletion.
  1. +1 −1 ext/nokogiri/xml_node.c
  2. +9 −0 test/xml/test_unparented_node.rb
View
@@ -84,7 +84,7 @@ static xmlNodePtr xmlReplaceNodeWrapper(xmlNodePtr pivot, xmlNodePtr new_node)
}
/* work around libxml2 issue: https://bugzilla.gnome.org/show_bug.cgi?id=615612 */
- if (retval->type == XML_TEXT_NODE) {
+ if (retval && retval->type == XML_TEXT_NODE) {
if (retval->prev && retval->prev->type == XML_TEXT_NODE) {
retval = xmlTextMerge(retval->prev, retval);
}
@@ -403,6 +403,15 @@ def test_replace
assert_equal set[0].to_xml, second.to_xml
end
+ def test_replace_on_unparented_node
+ foo = Node.new('foo', @node.document)
+ if Nokogiri.jruby? # JRuby Nokogiri doesn't raise an exception
+ @node.replace(foo)
+ else
+ assert_raises(RuntimeError){ @node.replace(foo) }
+ end
+ end
+
def test_illegal_replace_of_node_with_doc
new_node = Nokogiri::XML.parse('<foo>bar</foo>')
old_node = @node.at('.//employee')

0 comments on commit 5a09ab9

Please sign in to comment.