From 72942267aae0082e7816df73636d0c23a167fc28 Mon Sep 17 00:00:00 2001 From: David Maciejak Date: Wed, 14 Jan 2026 22:03:56 -0500 Subject: [PATCH] Prevent wrong UTF-8 window title truncation The patch is fixing a UTF-8 truncation issue that could happen with the window title when it was shrinked to be displayed in the window list (F11), leading to an infinite loop. Issue was reported at https://github.com/window-maker/wmaker/issues/61 --- src/misc.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/misc.c b/src/misc.c index ffd97e6e..4c920520 100644 --- a/src/misc.c +++ b/src/misc.c @@ -311,7 +311,8 @@ char *ShrinkString(WMFont *font, const char *string, int width) p1 = 0; p2 = p; t = (p2 - p1) / 2; - while (p2 > p1 && p1 != t) { + while (p2 > p1) { + int old_t = t; /* ensure we cut at UTF-8 character boundary */ utf8_safe_pos = utf8_find_char_start(string, p - t); t = p - utf8_safe_pos; @@ -319,12 +320,20 @@ char *ShrinkString(WMFont *font, const char *string, int width) w = WMWidthOfString(font, &string[utf8_safe_pos], t); if (w > width) { p2 = t; - t = p1 + (p2 - p1) / 2; } else if (w < width) { p1 = t; - t = p1 + (p2 - p1) / 2; - } else - p2 = p1 = t; + } else { + p1 = t; + break; + } + + /* Calculate new t for next iteration */ + t = p1 + (p2 - p1) / 2; + + /* Prevent infinite loop if we're not making progress */ + if (t == old_t || (p2 - p1) <= 1) { + break; + } } /* ensure final cut is at UTF-8 character boundary */