Skip to content

Commit

Permalink
Fix invalid hex escape in Regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Sep 20, 2024
1 parent fadd824 commit 22ee029
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
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

0 comments on commit 22ee029

Please sign in to comment.