[hackers] [st][PATCH] fix: realloc kpress buffer as needed.

From: yahei <yahei1423_AT_gmail.com>
Date: Mon, 12 May 2025 01:53:08 +0900

If XBufferOverflow occuers, the client should recall the function with
the same event and a buffer of adequate size to obtain the string.
Fixed as such.
---
 x.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/x.c b/x.c
index d73152b..b69523d 100644
--- a/x.c
+++ b/x.c
_AT_@ -1843,21 +1843,29 @@ kpress(XEvent *ev)
 {
 	XKeyEvent *e = &ev->xkey;
 	KeySym ksym = NoSymbol;
-	char buf[64], *customkey;
+	char *customkey;
+	static char *buf = NULL;
+	static int bufsize = 64;
 	int len;
 	Rune c;
 	Status status;
 	Shortcut *bp;
 
+	if (!buf)
+		buf = xmalloc(bufsize * sizeof(char));
+
 	if (IS_SET(MODE_KBDLOCK))
 		return;
 
 	if (xw.ime.xic) {
-		len = XmbLookupString(xw.ime.xic, e, buf, sizeof buf, &ksym, &status);
-		if (status == XBufferOverflow)
-			return;
+		len = XmbLookupString(xw.ime.xic, e, buf, bufsize, &ksym, &status);
+		if (status == XBufferOverflow) {
+			bufsize = len;
+			buf = xrealloc(buf, bufsize * sizeof(char));
+			XmbLookupString(xw.ime.xic, e, buf, bufsize, &ksym, &status);
+		}
 	} else {
-		len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
+		len = XLookupString(e, buf, bufsize, &ksym, NULL);
 	}
 	/* 1. shortcuts */
 	for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
-- 
2.49.0
Received on Sun May 11 2025 - 18:53:08 CEST

This archive was generated by hypermail 2.3.0 : Tue May 13 2025 - 03:12:35 CEST