Skip to content

Commit b5bf109

Browse files
makenowjustkou
andauthored
Add a "malformed comment" check for top-level comments (#145)
This check was missing. Therefore, `REXML::Document.new("<!--")` raised the ``undefined method `[]' for nil`` error, for example. This PR also adds tests for "malformed comment" checks. --------- Co-authored-by: Sutou Kouhei <[email protected]>
1 parent 6415113 commit b5bf109

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

lib/rexml/parsers/baseparser.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,14 @@ def pull_event
228228
return process_instruction(start_position)
229229
elsif @source.match("<!", true)
230230
if @source.match("--", true)
231-
return [ :comment, @source.match(/(.*?)-->/um, true)[1] ]
231+
md = @source.match(/(.*?)-->/um, true)
232+
if md.nil?
233+
raise REXML::ParseException.new("Unclosed comment", @source)
234+
end
235+
if /--|-\z/.match?(md[1])
236+
raise REXML::ParseException.new("Malformed comment", @source)
237+
end
238+
return [ :comment, md[1] ]
232239
elsif @source.match("DOCTYPE", true)
233240
base_error_message = "Malformed DOCTYPE"
234241
unless @source.match(/\s+/um, true)

test/parse/test_comment.rb

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require "test/unit"
2+
require "rexml/document"
3+
4+
module REXMLTests
5+
class TestParseComment < Test::Unit::TestCase
6+
def parse(xml)
7+
REXML::Document.new(xml)
8+
end
9+
10+
class TestInvalid < self
11+
def test_toplevel_unclosed_comment
12+
exception = assert_raise(REXML::ParseException) do
13+
parse("<!--")
14+
end
15+
assert_equal(<<~DETAIL, exception.to_s)
16+
Unclosed comment
17+
Line: 1
18+
Position: 4
19+
Last 80 unconsumed characters:
20+
DETAIL
21+
end
22+
23+
def test_toplevel_malformed_comment_inner
24+
exception = assert_raise(REXML::ParseException) do
25+
parse("<!-- -- -->")
26+
end
27+
assert_equal(<<~DETAIL, exception.to_s)
28+
Malformed comment
29+
Line: 1
30+
Position: 11
31+
Last 80 unconsumed characters:
32+
DETAIL
33+
end
34+
35+
def test_toplevel_malformed_comment_end
36+
exception = assert_raise(REXML::ParseException) do
37+
parse("<!-- --->")
38+
end
39+
assert_equal(<<~DETAIL, exception.to_s)
40+
Malformed comment
41+
Line: 1
42+
Position: 9
43+
Last 80 unconsumed characters:
44+
DETAIL
45+
end
46+
47+
def test_doctype_malformed_comment_inner
48+
exception = assert_raise(REXML::ParseException) do
49+
parse("<!DOCTYPE foo [<!-- -- -->")
50+
end
51+
assert_equal(<<~DETAIL, exception.to_s)
52+
Malformed comment
53+
Line: 1
54+
Position: 26
55+
Last 80 unconsumed characters:
56+
DETAIL
57+
end
58+
59+
def test_doctype_malformed_comment_end
60+
exception = assert_raise(REXML::ParseException) do
61+
parse("<!DOCTYPE foo [<!-- --->")
62+
end
63+
assert_equal(<<~DETAIL, exception.to_s)
64+
Malformed comment
65+
Line: 1
66+
Position: 24
67+
Last 80 unconsumed characters:
68+
DETAIL
69+
end
70+
71+
def test_after_doctype_malformed_comment_inner
72+
exception = assert_raise(REXML::ParseException) do
73+
parse("<a><!-- -- -->")
74+
end
75+
assert_equal(<<~DETAIL, exception.to_s)
76+
Malformed comment
77+
Line: 1
78+
Position: 14
79+
Last 80 unconsumed characters:
80+
DETAIL
81+
end
82+
83+
def test_after_doctype_malformed_comment_end
84+
exception = assert_raise(REXML::ParseException) do
85+
parse("<a><!-- --->")
86+
end
87+
assert_equal(<<~DETAIL, exception.to_s)
88+
Malformed comment
89+
Line: 1
90+
Position: 12
91+
Last 80 unconsumed characters:
92+
DETAIL
93+
end
94+
end
95+
end
96+
end

0 commit comments

Comments
 (0)