diff --git a/spec/core/regexp/shared/new.rb b/spec/core/regexp/shared/new.rb index 640f83f9a..44ce0a51f 100644 --- a/spec/core/regexp/shared/new.rb +++ b/spec/core/regexp/shared/new.rb @@ -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 diff --git a/src/regexp_object.cpp b/src/regexp_object.cpp index 21d7a4264..e3b471466 100644 --- a/src/regexp_object.cpp +++ b/src/regexp_object.cpp @@ -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; }