Skip to content

Commit

Permalink
Raise a RegexpError for empty or too large \u{} escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Sep 20, 2024
1 parent bde3657 commit 8a7666c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 2 additions & 6 deletions spec/core/regexp/shared/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,11 @@ def obj.to_int() ScratchPad.record(:called) end
end

it "raises a RegexpError if the \\u{} escape is empty" do
NATFIXME "raises a RegexpError if the \\u{} escape is empty", exception: SpecFailedException do
-> { Regexp.send(@method, "\\" + "u{}") }.should raise_error(RegexpError, Regexp.new(Regexp.escape("invalid Unicode list: /\\u{}/")))
end
-> { Regexp.send(@method, "\\" + "u{}") }.should raise_error(RegexpError, Regexp.new(Regexp.escape("invalid Unicode list: /\\u{}/")))
end

it "raises a RegexpError if more than six hexadecimal digits are given" do
NATFIXME "raises a RegexpError if more than six hexadecimal digits are given", exception: SpecFailedException do
-> { Regexp.send(@method, "\\" + "u{0ffffff}") }.should raise_error(RegexpError, Regexp.new(Regexp.escape("invalid Unicode range: /\\u{0ffffff}/")))
end
-> { Regexp.send(@method, "\\" + "u{0ffffff}") }.should raise_error(RegexpError, Regexp.new(Regexp.escape("invalid Unicode range: /\\u{0ffffff}/")))
end

it "returns a Regexp with US-ASCII encoding if only 7-bit ASCII characters are present regardless of the input String's encoding" do
Expand Down
7 changes: 7 additions & 0 deletions src/regexp_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ static String prepare_pattern_for_onigmo(Env *env, const StringObject *pattern,
// Convert \u{dd} to \udddd
case '{': {
c = next_char();
size_t num_digits = 0;
do {
long codepoint = 0;
while (isxdigit(c)) {
num_digits++;
codepoint *= 16;
if (isdigit(c))
codepoint += c - '0';
Expand All @@ -323,6 +325,11 @@ static String prepare_pattern_for_onigmo(Env *env, const StringObject *pattern,
while (c == ' ')
c = next_char();
} while (c != '}');
if (num_digits == 0) {
env->raise("RegexpError", "invalid Unicode list: /{}/", pattern->string());
} else if (num_digits > 6) {
env->raise("RegexpError", "invalid Unicode range: /{}/", pattern->string());
}
break;
}

Expand Down

0 comments on commit 8a7666c

Please sign in to comment.