From 449899b38314d0ecbe61e42d34398bdf2561b8e9 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 9 May 2024 11:56:51 +0200 Subject: Fix `String#index` to clear MatchData when a regexp is passed [Bug #20421] The bug was fixed in Ruby 3.3 via 9dcdffb8bf8a3654fd78bf1a58b30c8e13888a7a --- string.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'string.c') diff --git a/string.c b/string.c index 1f7321a1ce..ed51fa4af7 100644 --- a/string.c +++ b/string.c @@ -3993,8 +3993,10 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str) } if (RB_TYPE_P(sub, T_REGEXP)) { - if (pos > str_strlen(str, NULL)) + if (pos > str_strlen(str, NULL)) { + rb_backref_set(Qnil); return Qnil; + } pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos, rb_enc_check(str, sub), single_byte_optimizable(str)); -- cgit v1.2.3