[hackers] [dmenu][PATCH] insert: use long instead of ssize_t

From: NRK <nrk_AT_disroot.org>
Date: Sat, 3 Sep 2022 02:38:35 +0600

`ssize_t` is bit of an exotic type. by POSIX, it's only guaranteed to be
able to hold {-1, SSIZE_MAX} [0], meaning that negative values less than
-1 isn't guaranteed to representable in `ssize_t` (though it works out
in practice).

by default `ctrl+u` will call `insert()` with `-cursor` which can be
lesser than `-1`. so just use `long` instead which doesn't have such
issues.

[0]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html#tag_13_65
---
 dmenu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/dmenu.c b/dmenu.c
index 818313a..1817582 100644
--- a/dmenu.c
+++ b/dmenu.c
_AT_@ -282,7 +282,7 @@ match(void)
 }
 
 static void
-insert(const char *str, ssize_t n)
+insert(const char *str, long n)
 {
 	if (strlen(text) + n > sizeof text - 1)
 		return;
_AT_@ -363,7 +363,7 @@ keypress(XKeyEvent *ev)
 			match();
 			break;
 		case XK_u: /* delete left */
-			insert(NULL, 0 - cursor);
+			insert(NULL, 0 - (long)cursor);
 			break;
 		case XK_w: /* delete word */
 			while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)]))
_AT_@ -540,7 +540,7 @@ paste(void)
 	if (XGetWindowProperty(dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
 	                   utf8, &da, &di, &dl, &dl, (unsigned char **)&p)
 	    == Success && p) {
-		insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t)strlen(p));
+		insert(p, (q = strchr(p, '\n')) ? q - p : (long)strlen(p));
 		XFree(p);
 	}
 	drawmenu();
-- 
2.35.1
Received on Fri Sep 02 2022 - 22:38:35 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 02 2022 - 22:48:36 CEST