Skip to content

Commit

Permalink
Fix infinite loop with MATCH_INVALID_UTF in PCRE2 <10.36 (#13311)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Apr 13, 2023
1 parent 953ba96 commit c7d7042
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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

0 comments on commit c7d7042

Please sign in to comment.