string.c: check if the index points character boundary; fix #6267

This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-05-17 23:11:27 +09:00
parent f2e1cf2d3c
commit bd7efbe75f
+3 -2
View File
@@ -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++;
}
}