Skip to content

mruby-regexp: reject a hex escape with no digit - #7260

Merged
matz merged 1 commit into
mruby:masterfrom
takumin:regexp-hex-escape-no-digits
Aug 18, 2026
Merged

mruby-regexp: reject a hex escape with no digit#7260
matz merged 1 commit into
mruby:masterfrom
takumin:regexp-hex-escape-no-digits

Conversation

@takumin

@takumin takumin commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

parse_escape in re_compile.c reads \x by taking up to two hex digits and returning whatever it has, so \x followed by no digit came out as \x00. A pattern handed to Regexp.new then compiled quietly to a NUL (a regexp literal never gets this far, since the parser refuses /\x/ as a syntax error, as CRuby's does): \x{41} was a NUL and a {41} quantifier and matched 41 NUL bytes, \x and \xZ were a NUL, and [\x] held a NUL because the class reads its escapes through the same function. This is the spelling the README's "No \x{...} hex escape" note steers a reader away from, and it did not fail, it matched something else.

Regexp.new("\\x{41}") =~ "\0" * 41
# CRuby:        RegexpError (invalid hex escape: /\x{41}/)
# mruby before: 0
# mruby after:  RegexpError (invalid hex escape: /\x{41}/)

Regexp.new("\\x") =~ "\0"
# CRuby:        RegexpError (invalid hex escape: /\x/)
# mruby before: 0
# mruby after:  RegexpError (invalid hex escape: /\x/)

Regexp.new("[\\x]") =~ "\0"
# CRuby:        RegexpError (invalid hex escape: /[\x]/)
# mruby before: 0
# mruby after:  RegexpError (invalid hex escape: /[\x]/)

Regexp.new("\\x4Z") =~ "\x04Z"
# CRuby / mruby before / mruby after: 0

Fix

parse_escape raises RegexpError with CRuby's message, invalid hex escape, when the digit loop read nothing. One digit is still enough (\x4, \x4Z), as before, and the check sits in the shared function, so a class atom ([\x], [\xZ], [a-\x]) is refused by the same line. The README's limitation note now says \x{...} raises, since the brace is not a hex digit, and the escape list says \x with no digit raises.

The \x{...} form itself is still not implemented; this only makes the missing form an error rather than a silent NUL, which is what CRuby does with it too.

Testing

mrbgems/mruby-regexp/test/regexp_syntax.rb gains "a hex escape needs at least one digit": the message for \x{41}, \x, and [\x], plus \xZ, a\x, \x{}, [\xZ], [\x{41}], [a-\x], [\x-z] raising, and \x4, \x4Z, [\x4] still matching "\x04". Every pattern in the block was run against CRuby 4.0.6 first and behaves the same there.

Full suite green at every commit (one commit).

Build (MRUBY_CONFIG) Total KO Crash
ci/gcc-clang full-debug 2351 0 0
ci/gcc-clang bintest 2351 (+123 bintest) 0 0
ci/gcc-clang cxx_abi 2351 0 0
ci/gcc-clang byte-string 2281 0 0
ci/gcc-clang ascii-case 2348 0 0
default (unset) 2127 (+112 bintest) 0 0

Environment

Machine, toolchain, and the compile line of every build
Item Value
OS Ubuntu 24.04.4 LTS
Kernel 7.0.0-29-generic
CPU AMD Ryzen 9 5950X 16-Core Processor
C compiler gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
binutils GNU ld (GNU Binutils) 2.47.20260726
rake rake, version 13.3.1
CRuby (reference) ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [x86_64-linux]

Actual compile line of src/string.c in each build_config/ci/gcc-clang.rb build (-MMD -c, -I, and -o dropped). full-debug is -O0 because enable_debug appends -g3 -O0 after the toolchain's -g -O3; cxx_abi compiles C as C++ with gcc -x c++ -std=gnu++03, g++ only links.

# full-debug
gcc -std=gnu99 -g -O3 -Wall -Wundef -Werror-implicit-function-declaration -Wwrite-strings -g3 -O0 -DMRB_GC_STRESS -DMRB_USE_DEBUG_HOOK -DMRB_DEBUG -DMRB_USE_BIGINT -DMRB_USE_COMPLEX -DHAVE_MRUBY_ENCODING_GEM -DMRB_UTF8_STRING -DHAVE_MRUBY_IO_GEM -DMRB_USE_RATIONAL -DHAVE_MRUBY_REGEXP_GEM -DMRB_USE_SET -DMRB_USE_TASK_SCHEDULER src/string.c
# bintest
gcc -std=gnu99 -g -O3 -Wall -Wundef -Werror-implicit-function-declaration -Wwrite-strings -DMRB_GC_FIXED_ARENA -DMRB_USE_BIGINT -DMRB_USE_COMPLEX -DHAVE_MRUBY_ENCODING_GEM -DMRB_UTF8_STRING -DHAVE_MRUBY_IO_GEM -DMRB_USE_RATIONAL -DHAVE_MRUBY_REGEXP_GEM -DMRB_USE_SET -DMRB_USE_TASK_SCHEDULER -DMRB_USE_DEBUG_HOOK src/string.c
# cxx_abi
gcc -g -O3 -Wall -Wundef -Wwrite-strings -x c++ -std=gnu++03 -DMRB_GC_FIXED_ARENA -DMRB_USE_CXX_EXCEPTION -DMRB_USE_CXX_ABI -DMRB_USE_BIGINT -DMRB_USE_COMPLEX -DHAVE_MRUBY_ENCODING_GEM -DMRB_UTF8_STRING -DHAVE_MRUBY_IO_GEM -DMRB_USE_RATIONAL -DHAVE_MRUBY_REGEXP_GEM -DMRB_USE_SET -DMRB_USE_TASK_SCHEDULER src/string.c
# byte-string
gcc -std=gnu99 -g -O3 -Wall -Wundef -Werror-implicit-function-declaration -Wwrite-strings -DMRB_USE_BIGINT -DMRB_USE_COMPLEX -DHAVE_MRUBY_IO_GEM -DMRB_USE_RATIONAL -DHAVE_MRUBY_REGEXP_GEM -DMRB_USE_SET -DMRB_USE_TASK_SCHEDULER src/string.c
# ascii-case
gcc -std=gnu99 -g -O3 -Wall -Wundef -Werror-implicit-function-declaration -Wwrite-strings -DMRB_USE_ASCII_CASE -DMRB_USE_BIGINT -DMRB_USE_COMPLEX -DHAVE_MRUBY_ENCODING_GEM -DMRB_UTF8_STRING -DHAVE_MRUBY_IO_GEM -DMRB_USE_RATIONAL -DHAVE_MRUBY_REGEXP_GEM -DMRB_USE_SET -DMRB_USE_TASK_SCHEDULER src/string.c

Summary by CodeRabbit

  • Bug Fixes

    • Improved regular expression validation for hexadecimal escapes.
    • Invalid forms such as bare \x, \xZ, and \x{...} now raise RegexpError.
    • Valid one- and two-digit hexadecimal escapes continue to work as expected.
  • Documentation

    • Clarified supported hexadecimal and Unicode escape syntax.

`parse_escape` read `\x` by taking up to two hex digits and returning
whatever it had, so `\x` followed by none came out as `\x00`. A pattern
handed to `Regexp.new` (a literal never gets this far, since the parser
refuses it) then compiled quietly to a NUL: `\x{41}` was a NUL and a
quantifier and matched 41 NUL bytes, and `\x` or `\xZ` was a NUL as well.
The same path serves a character class, so `[\x]` held a NUL. CRuby
raises for each of them.

```ruby
Regexp.new("\\x{41}") =~ "\0" * 41  # CRuby: RegexpError (invalid hex escape: /\x{41}/), mruby: 0
Regexp.new("\\x") =~ "\0"           # CRuby: RegexpError (invalid hex escape: /\x/), mruby: 0
Regexp.new("[\\x]") =~ "\0"         # CRuby: RegexpError (invalid hex escape: /[\x]/), mruby: 0
```

Raise `RegexpError` with CRuby's message when the escape reads no digit.
`\x4` and `\x4Z` still read one digit, as before. The README already says
there is no `\x{...}` escape; it now says that spelling raises, since the
brace is not a digit, rather than leaving the reader to find out what
`\x` alone comes to.
@takumin
takumin requested a review from matz as a code owner August 18, 2026 10:09
@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 39a813df-77f9-4a23-b959-52b33f74809e

📥 Commits

Reviewing files that changed from the base of the PR and between 885e215 and dbfda0e.

📒 Files selected for processing (3)
  • mrbgems/mruby-regexp/README.md
  • mrbgems/mruby-regexp/src/re_compile.c
  • mrbgems/mruby-regexp/test/regexp_syntax.rb

Included review availability: Your plan includes up to 8 reviews per rolling hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

The regexp compiler now requires at least one hexadecimal digit after \x. Invalid forms raise RegexpError. Documentation and regression tests describe and verify this behavior.

Changes

Regexp hexadecimal escape validation

Layer / File(s) Summary
Hexadecimal escape parsing and documentation
mrbgems/mruby-regexp/src/re_compile.c, mrbgems/mruby-regexp/README.md
\x escapes without hexadecimal digits now raise RegexpError. Documentation clarifies braced forms and recommends \u{...} for codepoints above 0xff.
Hexadecimal escape regression coverage
mrbgems/mruby-regexp/test/regexp_syntax.rb
Tests cover invalid escapes, character classes and ranges, valid one-digit escapes, and termination at non-hexadecimal characters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to dbfda

This localized change rejects malformed hexadecimal regexp escapes instead of silently compiling them as NUL bytes, with matching tests and documentation updates. No actionable merge-blocking risk remains after normal checks and review.

Possibly related PRs

  • mruby/mruby#7074: Updates related hexadecimal escape parsing in re_compile.c, including \u parsing.

Suggested reviewers: matz

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: rejecting hexadecimal escapes that contain no digit.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@matz
matz merged commit f20efec into mruby:master Aug 18, 2026
21 checks passed
@takumin
takumin deleted the regexp-hex-escape-no-digits branch August 19, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants