Skip to content

Commit

Permalink
Fix a segfault when replacing an unparented node.
Browse files Browse the repository at this point in the history
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
ender672 committed Sep 12, 2012
1 parent 7aefe8a commit 5a09ab9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions test/xml/test_unparented_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 5a09ab9

Please sign in to comment.