Skip to content

Commit

Permalink
XML::Attr.new checks type of Document arg
Browse files Browse the repository at this point in the history
to avoid unhelpfully segfaulting when dereferencing a pointer to
something not-a-Document

closes #1477
  • Loading branch information
flavorjones committed Jan 13, 2017
1 parent 3330d35 commit 5da1984
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ext/nokogiri/xml_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ static VALUE new(int argc, VALUE *argv, VALUE klass)

rb_scan_args(argc, argv, "2*", &document, &name, &rest);

if (! rb_obj_is_kind_of(document, cNokogiriXmlDocument)) {
rb_raise(rb_eArgError, "parameter must be a Nokogiri::XML::Document");
}

Data_Get_Struct(document, xmlDoc, xml_doc);

node = xmlNewDocProp(
Expand Down
7 changes: 7 additions & 0 deletions test/xml/test_attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ def test_new
}
end

def test_new_raises_argerror_on_nondocument
document = Nokogiri::XML "<root><foo/></root>"
assert_raises ArgumentError do
Nokogiri::XML::Attr.new document.at_css("foo"), "bar"
end
end

def test_content=
xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
address = xml.xpath('//address')[3]
Expand Down

0 comments on commit 5da1984

Please sign in to comment.