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 invalid hex escape in Regexp #2228

Merged
merged 1 commit into from
Sep 20, 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
4 changes: 1 addition & 3 deletions spec/core/regexp/shared/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,7 @@ def obj.to_int() ScratchPad.record(:called) end
end

it "raises a RegexpError if \\x is not followed by any hexadecimal digits" do
NATFIXME "raises a RegexpError if \\x is not followed by any hexadecimal digits", exception: SpecFailedException do
-> { Regexp.send(@method, "\\" + "xn") }.should raise_error(RegexpError, Regexp.new(Regexp.escape("invalid hex escape: /\\xn/")))
end
-> { Regexp.send(@method, "\\" + "xn") }.should raise_error(RegexpError, Regexp.new(Regexp.escape("invalid hex escape: /\\xn/")))
end

it "accepts an escaped string interpolation" do
Expand Down
6 changes: 6 additions & 0 deletions src/regexp_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,15 @@ static String prepare_pattern_for_onigmo(Env *env, const StringObject *pattern,
}

case 'x': {
c = next_char();
if (!std::isxdigit(c))
env->raise("RegexpError", "invalid hex escape: /{}/", pattern->string());

*fixed_encoding = true;
new_pattern.append_char('\\');
new_pattern.append_char('x');
new_pattern.append_char(c);

break;
}

Expand Down
Loading