Project

General

Profile

Actions

Bug #22717

open

MatchData#initialize_copy keeps a stale character offsets cache

Bug #22717: MatchData#initialize_copy keeps a stale character offsets cache

Added by saddestmartian (sad mart) 1 day ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-12-03T23:00:19Z master bcd243e8e8) +PRISM [x86_64-linux]
[ruby-core:127845]

Description

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:

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

No data to display

Actions

Also available in: PDF Atom