Skip to content

Commit

Permalink
Fix to handle unclosed doctype decls in BaseParser
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Jun 19, 2024
1 parent 7e68d5a commit 7e5e2fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
10 changes: 9 additions & 1 deletion lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,12 @@ def pull_event
x, @closed = @closed, nil
return [ :end_element, x ]
end
return [ :end_document ] if empty?
if empty?
if @document_status == :in_doctype
raise ParseException.new("Malformed DOCTYPE: unclosed", @source)
end
return [ :end_document ]
end
return @stack.shift if @stack.size > 0
#STDERR.puts @source.encoding
#STDERR.puts "BUFFER = #{@source.buffer.inspect}"
Expand Down Expand Up @@ -373,6 +378,9 @@ def pull_event
@document_status = :after_doctype
return [ :end_doctype ]
end
if @document_status == :in_doctype
raise ParseException.new("Malformed DOCTYPE: invalid declaration", @source)
end
end
if @document_status == :after_doctype
@source.match(/\s*/um, true)
Expand Down
12 changes: 0 additions & 12 deletions lib/rexml/parsers/treeparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@ def parse
#STDERR.puts "TREEPARSER GOT #{event.inspect}"
case event[0]
when :end_document
if in_doctype
raise ParseException.new("Malformed DOCTYPE: unclosed",
@parser.source, @parser)
end
unless tag_stack.empty?
raise ParseException.new("No close tag for #{@build_context.xpath}",
@parser.source, @parser)
end
return
when :start_element
if in_doctype
raise ParseException.new("Malformed DOCTYPE: unclosed",
@parser.source, @parser)
end
tag_stack.push(event[1])
el = @build_context = @build_context.add_element( event[1] )
event[2].each do |key, value|
Expand All @@ -47,10 +39,6 @@ def parse
tag_stack.pop
@build_context = @build_context.parent
when :text
if in_doctype
raise ParseException.new("Malformed DOCTYPE: unclosed",
@parser.source, @parser)
end
if @build_context[-1].instance_of? Text
@build_context[-1] << event[1]
else
Expand Down
14 changes: 6 additions & 8 deletions test/parse/test_document_type_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,12 @@ def test_no_name
class TestUnclosed < self
def test_no_extra_node
exception = assert_raise(REXML::ParseException) do
REXML::Document.new(<<~DOCTYPE)
<!DOCTYPE foo [
DOCTYPE
REXML::Document.new("<!DOCTYPE foo [")
end
assert_equal(<<~DETAIL.chomp, exception.to_s)
Malformed DOCTYPE: unclosed
Line: 1
Position: 16
Position: 15
Last 80 unconsumed characters:
DETAIL
Expand All @@ -76,11 +74,11 @@ def test_start_element
DOCTYPE
end
assert_equal(<<~DETAIL.chomp, exception.to_s)
Malformed DOCTYPE: unclosed
Malformed DOCTYPE: invalid declaration
Line: 1
Position: 20
Last 80 unconsumed characters:
#{' '}
<r>#{' '}
DETAIL
end

Expand All @@ -91,11 +89,11 @@ def test_text
DOCTYPE
end
assert_equal(<<~DETAIL.chomp, exception.to_s)
Malformed DOCTYPE: unclosed
Malformed DOCTYPE: invalid declaration
Line: 1
Position: 21
Last 80 unconsumed characters:
text#{' '}
DETAIL
end
end
Expand Down

0 comments on commit 7e5e2fc

Please sign in to comment.