Skip to content

Fix performance issue caused by using repeated > characters inside comments #171

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

Merged
merged 2 commits into from
Jul 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class BaseParser
module Private
INSTRUCTION_END = /#{NAME}(\s+.*?)?\?>/um
INSTRUCTION_TERM = "?>"
COMMENT_TERM = "-->"
TAG_PATTERN = /((?>#{QNAME_STR}))\s*/um
CLOSE_PATTERN = /(#{QNAME_STR})\s*>/um
ATTLISTDECL_END = /\s+#{NAME}(?:#{ATTDEF})*\s*>/um
Expand Down Expand Up @@ -243,7 +244,7 @@ def pull_event
return process_instruction(start_position)
elsif @source.match("<!", true)
if @source.match("--", true)
md = @source.match(/(.*?)-->/um, true)
md = @source.match(/(.*?)-->/um, true, term: Private::COMMENT_TERM)
if md.nil?
raise REXML::ParseException.new("Unclosed comment", @source)
end
Expand Down
11 changes: 11 additions & 0 deletions test/parse/test_comment.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
require "test/unit"
require "core_assertions"

require "rexml/document"

module REXMLTests
class TestParseComment < Test::Unit::TestCase
include Test::Unit::CoreAssertions

def parse(xml)
REXML::Document.new(xml)
end
Expand Down Expand Up @@ -117,5 +121,12 @@ def test_after_root

assert_equal(" ok comment ", events[:comment])
end

def test_gt_linear_performance
seq = [10000, 50000, 100000, 150000, 200000]
assert_linear_performance(seq, rehearsal: 10) do |n|
REXML::Document.new('<!-- ' + ">" * n + ' -->')
end
end
end
end