[hackers] [dwm][PATCH] Fix wrong computation in drw_text

From: <suckless_AT_jochen.sprickerhof.de>
Date: Mon, 16 Aug 2021 19:17:38 +0200

From: Jochen Sprickerhof <suckless_AT_jochen.sprickerhof.de>

In case lpad * 2 > w, w - lpad * 2 wraps around and comparing it with ew
is always true.
Also shorten the text if the initial width does not fit with padding.
---
 drw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drw.c b/drw.c
index 9c39086..36b56df 100644
--- a/drw.c
+++ b/drw.c
_AT_@ -310,8 +310,8 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
 		if (utf8strlen) {
 			drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
 			/* shorten text if necessary */
-			if (ew > w)
-				for (ew = 0, len = 0; ew < w - lpad * 2 && len < MIN(utf8strlen, sizeof(buf) - 1); len++)
+			if (ew + lpad * 2 > w)
+				for (ew = 0, len = 0; ew + lpad * 2 < w && len < MIN(utf8strlen, sizeof(buf) - 1); len++)
 					drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
 			else
 				len = MIN(utf8strlen, sizeof(buf) - 1);
-- 
2.32.0
Received on Mon Aug 16 2021 - 19:17:38 CEST

This archive was generated by hypermail 2.3.0 : Tue Aug 17 2021 - 09:48:35 CEST