diff --git a/src/chars.c b/src/chars.c index 82d06c93566be9c1d8868f72899e16e11c858d84..ba134d5eefeaa7492853660585c75471ba4731a8 100644 --- a/src/chars.c +++ b/src/chars.c @@ -535,29 +535,29 @@ char *mbrevstrcasestr(const char *haystack, const char *needle, { #ifdef ENABLE_UTF8 if (use_utf8) { - size_t tail_len, needle_len; + size_t needle_len = mbstrlen(needle); + size_t tail_len = mbstrlen(pointer); - if (*needle == '\0') + if (needle_len == 0) return (char *)pointer; - needle_len = mbstrlen(needle); - if (mbstrlen(haystack) < needle_len) return NULL; - tail_len = mbstrlen(pointer); + if (tail_len < needle_len) + pointer += tail_len - needle_len; + + if (pointer < haystack) + return NULL; while (TRUE) { - if (tail_len >= needle_len && - mbstrncasecmp(pointer, needle, needle_len) == 0) + if (mbstrncasecmp(pointer, needle, needle_len) == 0) return (char *)pointer; - /* If we've reached the head of the haystack, we found nothing. */ if (pointer == haystack) return NULL; pointer = haystack + move_mbleft(haystack, pointer - haystack); - tail_len++; } } else #endif