Skip to content
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

Fix infinite loop with MATCH_INVALID_UTF in PCRE2 <10.36 #13311

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
10 changes: 5 additions & 5 deletions spec/std/regex_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ describe "Regex" do
/([\w_\.@#\/\*])+/.match("\xFF\xFE")
end
{% else %}
if Regex::PCRE2.version_number < {10, 35}
pending! "Error in libpcre2 < 10.35"
if Regex::PCRE2.version_number < {10, 36}
pending! "Error in libpcre2 < 10.36"
else
/([\w_\.@#\/\*])+/.match("\xFF\xFE").should be_nil
end
Expand Down Expand Up @@ -154,7 +154,7 @@ describe "Regex" do
end

it "multibyte index" do
if Regex::Engine.version_number < {10, 34}
if Regex::Engine.version_number < {10, 36}
expect_raises(ArgumentError, "bad offset into UTF string") do
/foo/.match_at_byte_index("öfoo", 1)
end
Expand Down Expand Up @@ -246,7 +246,7 @@ describe "Regex" do
end

it "invalid codepoint" do
if Regex::Engine.version_number < {10, 34}
if Regex::Engine.version_number < {10, 36}
expect_raises(ArgumentError, "UTF-8 error") do
/foo/.matches?("f\x96o")
end
Expand Down Expand Up @@ -310,7 +310,7 @@ describe "Regex" do
end

it "multibyte index" do
if Regex::Engine.version_number < {10, 34}
if Regex::Engine.version_number < {10, 36}
expect_raises(ArgumentError, "bad offset into UTF string") do
/foo/.matches_at_byte_index?("öfoo", 1)
end
Expand Down
4 changes: 3 additions & 1 deletion src/regex/pcre2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ module Regex::PCRE2
# :nodoc:
def initialize(*, _source @source : String, _options @options)
options = pcre2_compile_options(options) | LibPCRE2::UTF | LibPCRE2::DUPNAMES | LibPCRE2::UCP
if PCRE2.version_number >= {10, 34}
# MATCH_INVALID_UTF was introduced in 10.34 but a bug that can lead to an
# infinite loop is only fixed in 10.36 (https://github.com/PCRE2Project/pcre2/commit/e0c6029a62db9c2161941ecdf459205382d4d379).
if PCRE2.version_number >= {10, 36}
options |= LibPCRE2::MATCH_INVALID_UTF
end
@re = PCRE2.compile(source, options) do |error_message|
Expand Down