Skip to content

Check Regexp state before setting options in rb_reg_initialize - #10126

Draft
zev wants to merge 1 commit into
jruby:jruby-10.0from
zev:regexp-initialize-check
Draft

zev wants to merge 1 commit into
jruby:jruby-10.0from
zev:regexp-initialize-check

Conversation

@zev

@zev zev commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Regexp#initialize changed the options and timeout of an already initialized Regexp before raising TypeError, so a failed re-initialize still modified the Regexp:

Call CRuby 3.4 / 4.0 JRuby before JRuby after
r = Class.new(Regexp).new("a")
r.send(:initialize, "b", Regexp::IGNORECASE) rescue nil
r
/a/ /a/i /a/
...then r.timeout after timeout: 3 nil 3.0 nil

Changes

RubyRegexp#regexpInitialize (rb_reg_initialize) now runs the frozen and already-initialized checks before it sets options and timeout, as CRuby does in rb_reg_initialize_check.

This matters more in Ruby 4.1, where every Regexp is frozen (Feature #8948): without the fix, re-initializing a frozen Regexp would change it before FrozenError is raised. The ruby-4.1 freeze PR carries the same commit.

Specs

Added to core/regexp/initialize_spec.rb: re-initializing a Regexp subclass instance raises TypeError and leaves its options and timeout unchanged. No version guard; it passes on CRuby 3.3, 4.0 and 4.1.

Testing

  • core/regexp/initialize_spec.rb: passes on JRuby (interpreter and forced JIT), CRuby 3.3.12 and 4.0.7. On CRuby 4.1.0-dev the new example passes; the existing "raises a TypeError on an initialized non-literal Regexp" example fails there because 4.1 freezes Regexp.new (it is version-guarded on ruby-4.1).
  • mspec ci spec/ruby/core/regexp: 0 failures.
  • test/mri/ruby/test_regexp.rb: 0 failures.

🤖 Generated with Claude Code

regexpInitialize set the new options and timeout before it checked
whether the Regexp was frozen or already initialized. Re-initializing
an initialized Regexp raised TypeError, but the Regexp kept the new
options, so `r.send(:initialize, "b", Regexp::IGNORECASE)` turned /a/
into /a/i. CRuby runs rb_reg_initialize_check first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zev

zev commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Review and merge order for the Regexp freeze work (Feature #8948):

  1. Check Regexp state before setting options in rb_reg_initialize #10126 (jruby-10.0): check the Regexp state before setting options in rb_reg_initialize. It's independent and can merge any time. ← this PR
  2. Update test_regexp.rb and test_marshal.rb from CRuby #10127 (ruby-4.1): sync test_regexp.rb and test_marshal.rb from CRuby, with excludes.
  3. Freeze all instances of Regexp, except subclass instances #10128 (ruby-4.1): freeze Regexp instances. It's stacked on Update test_regexp.rb and test_marshal.rb from CRuby #10127, so merge that first. It also carries the Check Regexp state before setting options in rb_reg_initialize #10126 commit, because the freeze depends on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant