From 97e6ad49a4604e7e4ca04da2aaafc63cbd5d29d8 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 24 Feb 2025 14:42:09 +0100 Subject: [PATCH] Reuse the backref if it isn't marked as busy. [Misc #20652] --- re.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/re.c b/re.c index 4f7afb6b0b..5d365edb63 100644 --- a/re.c +++ b/re.c @@ -1813,11 +1813,20 @@ rb_reg_search_set_match(VALUE re, VALUE str, long pos, int reverse, int set_back return ONIG_MISMATCH; } - VALUE match; - if (set_match && RTEST(*set_match)) { + VALUE match = Qnil; + if (set_match) { match = *set_match; } - else { + + if (NIL_P(match)) { + match = rb_backref_get(); + } + + if (!NIL_P(match) && FL_TEST(match, MATCH_BUSY)) { + match = Qnil; + } + + if (NIL_P(match)) { match = match_alloc(rb_cMatch); }