From bd7efbe75fbf96d9bf58b4b38a8ddfb3a0d1e2d0 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 17 May 2024 23:11:27 +0900 Subject: [PATCH] string.c: check if the index points character boundary; fix #6267 --- src/string.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/string.c b/src/string.c index ae0cf20ac..e85c37401 100644 --- a/src/string.c +++ b/src/string.c @@ -501,10 +501,11 @@ bytes2chars(mrb_value s, mrb_int bi) } const char *p = RSTRING_PTR(s); + const char *e = p + RSTRING_LEN(s); const char *pivot = p + bi; mrb_int i = 0; - if (RSTRING_END(s) < pivot) return -1; + if (e < pivot) return -1; while (p < pivot) { if ((*p & 0x80) == 0) { const char *np = search_nonascii(p, pivot); @@ -512,7 +513,7 @@ bytes2chars(mrb_value s, mrb_int bi) p = np; } else { - p += mrb_utf8len(p, pivot); + p += mrb_utf8len(p, e); i++; } }