Skip to content

Commit

Permalink
fix(cruby): XML::Reader#attribute_hash returns nil on error
Browse files Browse the repository at this point in the history
Note that on JRuby, the namespaces are still returned because the
parse error would raised on the subsequent node expansion.

This restores the behavior from v1.13.7
  • Loading branch information
flavorjones committed Dec 7, 2022
1 parent 3b9c736 commit 9fe0761
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

---

## next / unreleased

### Improvements

* [CRuby] `XML::Reader#attribute_hash` now returns `nil` on parse errors. This restores the behavior of `#attributes` from v1.13.7 and earlier. [[#2715](https://github.com/sparklemotion/nokogiri/issues/2715)]


## 1.13.9 / 2022-10-18

### Security
Expand Down
4 changes: 4 additions & 0 deletions ext/nokogiri/xml_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ rb_xml_reader_attribute_hash(VALUE rb_reader)
}

c_node = xmlTextReaderExpand(c_reader);
if (c_node == NULL) {
return Qnil;
}

c_property = c_node->properties;
while (c_property != NULL) {
VALUE rb_name = NOKOGIRI_STR_NEW2(c_property->name);
Expand Down
32 changes: 32 additions & 0 deletions test/xml/test_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,38 @@ def test_nonexistent_attribute
reader.read # el
assert_nil(reader.attribute("other"))
end

def test_broken_markup_attribute_hash
xml = <<~XML
<root><foo bar="asdf" xmlns:quux="qwer">
XML
reader = Nokogiri::XML::Reader(xml)
reader.read # root
reader.read # foo

assert_equal("foo", reader.name)
if Nokogiri.jruby?
assert_equal({ "bar" => "asdf" }, reader.attribute_hash)
else
assert_nil(reader.attribute_hash)
end
end

def test_broken_markup_namespaces
xml = <<~XML
<root><foo bar="asdf" xmlns:quux="qwer">
XML
reader = Nokogiri::XML::Reader(xml)
reader.read # root
reader.read # foo

assert_equal("foo", reader.name)
if Nokogiri.jruby?
assert_equal({ "xmlns:quux" => "qwer" }, reader.namespaces)
else
assert_nil(reader.namespaces)
end
end
end
end
end

0 comments on commit 9fe0761

Please sign in to comment.