From e8d89b7c2d1daa3a429184b65cc8e02e442146a5 Mon Sep 17 00:00:00 2001
From: Jeff Pace <jeugenepace@gmail.com>
Date: Sat, 22 Oct 2011 17:40:12 -0400
Subject: [PATCH 1/2] JRUBY-6141 - fixed so that MatchData are cloned, so that
 references to MatchData are not updated with subsequent matches.

---
 src/org/jruby/RubyRegexp.java |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/src/org/jruby/RubyRegexp.java b/src/org/jruby/RubyRegexp.java
index 4f38f6c..73096b0 100644
--- a/src/org/jruby/RubyRegexp.java
+++ b/src/org/jruby/RubyRegexp.java
@@ -1572,7 +1572,12 @@ public class RubyRegexp extends RubyObject implements ReOptions, EncodingCapable
         // FIXME: This is pretty gross; we should have a cleaner initialization
         // that doesn't depend on package-visible fields and ideally is atomic,
         // probably using an immutable structure we replace all at once.
-        match.regs = matcher.getRegion(); // lazy, null when no groups defined
+
+        // The region must be cloned because a subsequent match will update the
+        // region, resulting in the MatchData created here pointing at the
+        // incorrect region (capture/group).
+        Region region = matcher.getRegion(); // lazy, null when no groups defined
+        match.regs = region == null ? null : region.clone();
         match.begin = matcher.getBegin();
         match.end = matcher.getEnd();
         match.pattern = pattern;
-- 
1.7.4.1

