From 0b7822581f460c2d9afd07ded4db8ad93c9b9cc8 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 27 Jun 2022 10:38:44 +0900 Subject: [PATCH] string.c (str_rindex): use more local variables; ref #5734 --- src/string.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/string.c b/src/string.c index 262153107..801cbc308 100644 --- a/src/string.c +++ b/src/string.c @@ -640,21 +640,22 @@ str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos) const char *s, *sbeg, *send, *t; struct RString *ps = mrb_str_ptr(str); mrb_int len = RSTRING_LEN(sub); + mrb_int slen = RSTR_LEN(ps); /* substring longer than string */ - if (RSTR_LEN(ps) < len) return -1; - if (RSTR_LEN(ps) - pos < len) { - pos = RSTR_LEN(ps) - len; + if (slen < len) return -1; + if (slen - pos < len) { + pos = slen - len; } sbeg = RSTR_PTR(ps); - send = sbeg + RSTR_LEN(ps); - s = RSTR_PTR(ps) + pos; + send = sbeg + slen; + s = sbeg + pos; t = RSTRING_PTR(sub); if (len) { s = char_adjust(sbeg, send, s); while (sbeg <= s) { if ((mrb_int)(send - s) >= len && memcmp(s, t, len) == 0) { - return (mrb_int)(s - RSTR_PTR(ps)); + return (mrb_int)(s - sbeg); } s = char_backtrack(sbeg, s); }