From: "saddestmartian (sad mart) via ruby-core" Date: 2026-12-04T05:44:21+00:00 Subject: [ruby-core:127845] [Ruby Bug#22717] MatchData#initialize_copy keeps a stale character offsets cache Issue #22717 has been reported by saddestmartian (sad mart). ---------------------------------------- Bug #22717: MatchData#initialize_copy keeps a stale character offsets cache https://bugs.ruby-lang.org/issues/22717 * Author: saddestmartian (sad mart) * Status: Open * ruby -v: ruby 4.1.0dev (2026-12-03T23:00:19Z master bcd243e8e8) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- `rb_match_init_copy` overwrites the receiver's registers but copies the character offsets cache only when the original has one, so a receiver that had already computed offsets keeps them: ```ruby m = /(b)/.match("ab"); m.offset(1) # builds the cache m.send(:initialize_copy, /(b)/.match("xxab")) m.offset(1) #=> [1, 2], expected [3, 4] ``` When the original has more groups than the receiver had, `offset`/`begin`/`end` read past the end of the cache. This is the same invariant as Bug #22638, on the one path its fix (9d4d6c3c14) did not cover. It reproduces on master and 4.0.10. A one-line fix with a test: https://github.com/ruby/ruby/pull/20474 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/