Skip to content

Commit

Permalink
Merge pull request #2228 from herwinw/regexp_invalid_hex_escape
Browse files Browse the repository at this point in the history
Fix invalid hex escape in Regexp
  • Loading branch information
herwinw committed Sep 20, 2024
2 parents 80ada26 + 22ee029 commit 3e85917
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 3e85917

Please sign in to comment.