Skip to content

Commit d906ae2

Browse files
authored
Add a "Malformed comment" check for invalid comments such as <!--> (#147)
`Document.new("<a><!-->")` raises `undefined method '[]' for nil`. This commit fixes it and adds a test for it.
1 parent 1e31ffc commit d906ae2

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/rexml/parsers/baseparser.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,11 @@ def pull_event
406406
if md[0][0] == ?-
407407
md = @source.match(/--(.*?)-->/um, true)
408408

409-
case md[1]
410-
when /--/, /-\z/
409+
if md.nil? || /--|-\z/.match?(md[1])
411410
raise REXML::ParseException.new("Malformed comment", @source)
412411
end
413412

414-
return [ :comment, md[1] ] if md
413+
return [ :comment, md[1] ]
415414
else
416415
md = @source.match(/\[CDATA\[(.*?)\]\]>/um, true)
417416
return [ :cdata, md[1] ] if md

test/parse/test_comment.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ def test_doctype_malformed_comment_end
6868
DETAIL
6969
end
7070

71+
def test_after_doctype_malformed_comment_short
72+
exception = assert_raise(REXML::ParseException) do
73+
parse("<a><!-->")
74+
end
75+
assert_equal(<<~DETAIL.chomp, exception.to_s)
76+
Malformed comment
77+
Line: 1
78+
Position: 8
79+
Last 80 unconsumed characters:
80+
-->
81+
DETAIL
82+
end
83+
7184
def test_after_doctype_malformed_comment_inner
7285
exception = assert_raise(REXML::ParseException) do
7386
parse("<a><!-- -- -->")

0 commit comments

Comments
 (0)