Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: XML::Reader#attribute_hash returns nil on error #2715

Merged
merged 1 commit into from Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
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
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
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