Skip to content

Commit 9fe0761

Browse files
committed
fix(cruby): XML::Reader#attribute_hash returns nil on error
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
1 parent 3b9c736 commit 9fe0761

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA
44

55
---
66

7+
## next / unreleased
8+
9+
### Improvements
10+
11+
* [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)]
12+
13+
714
## 1.13.9 / 2022-10-18
815

916
### Security

ext/nokogiri/xml_reader.c

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ rb_xml_reader_attribute_hash(VALUE rb_reader)
212212
}
213213

214214
c_node = xmlTextReaderExpand(c_reader);
215+
if (c_node == NULL) {
216+
return Qnil;
217+
}
218+
215219
c_property = c_node->properties;
216220
while (c_property != NULL) {
217221
VALUE rb_name = NOKOGIRI_STR_NEW2(c_property->name);

test/xml/test_reader.rb

+32
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,38 @@ def test_nonexistent_attribute
681681
reader.read # el
682682
assert_nil(reader.attribute("other"))
683683
end
684+
685+
def test_broken_markup_attribute_hash
686+
xml = <<~XML
687+
<root><foo bar="asdf" xmlns:quux="qwer">
688+
XML
689+
reader = Nokogiri::XML::Reader(xml)
690+
reader.read # root
691+
reader.read # foo
692+
693+
assert_equal("foo", reader.name)
694+
if Nokogiri.jruby?
695+
assert_equal({ "bar" => "asdf" }, reader.attribute_hash)
696+
else
697+
assert_nil(reader.attribute_hash)
698+
end
699+
end
700+
701+
def test_broken_markup_namespaces
702+
xml = <<~XML
703+
<root><foo bar="asdf" xmlns:quux="qwer">
704+
XML
705+
reader = Nokogiri::XML::Reader(xml)
706+
reader.read # root
707+
reader.read # foo
708+
709+
assert_equal("foo", reader.name)
710+
if Nokogiri.jruby?
711+
assert_equal({ "xmlns:quux" => "qwer" }, reader.namespaces)
712+
else
713+
assert_nil(reader.namespaces)
714+
end
715+
end
684716
end
685717
end
686718
end

0 commit comments

Comments
 (0)