From 64a4bf0e28fdebda2e82652208d9eb3c9db728e3 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 9 Jan 2024 10:19:37 +0900 Subject: [PATCH] string.c: avoid infinite loop; fix #6143 `mrb_utf8len` returns 0 for invalid characters. --- src/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/string.c b/src/string.c index 47a6ebfe0..5c770ec11 100644 --- a/src/string.c +++ b/src/string.c @@ -409,7 +409,10 @@ str_index_str_by_char_search(mrb_state *mrb, const char *p, const char *pend, co if (pivot >= pend || pivot < p /* overflowed */) { return -1; } do { - p += mrb_utf8len(p, pend); + mrb_int len = mrb_utf8len(p, pend); + + if (len == 0) p++; /* invalid character */ + else p += len; off++; } while (p < pivot); }