summary refs log tree commit diff
diff options
context:
space:
mode:
authorCurtis McEnroe2018-08-07 00:12:08 -0400
committerCurtis McEnroe2018-08-07 00:12:08 -0400
commitf1b1ffe79fb17e4228d9fb0a10aaba299b15548b (patch)
tree4ca5a4bf44a77e689055de9f19ac20df5db3bdfe
parenta26a6fee8d33dd583fa4fd3400387a8c05bd3fb5 (diff)
Make safe filling the who buffer
-rw-r--r--handle.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/handle.c b/handle.c
index c602c23..7fba7f2 100644
--- a/handle.c
+++ b/handle.c
@@ -152,8 +152,10 @@ static void handle366(char *prefix, char *params) {
 	ircFmt("WHO %s\r\n", chan);
 }
 
-static char whoBuf[4096];
-static size_t whoLen;
+static struct {
+	char buf[4096];
+	size_t len;
+} who;
 
 static void handle352(char *prefix, char *params) {
 	(void)prefix;
@@ -163,21 +165,23 @@ static void handle352(char *prefix, char *params) {
 	shift(&params);
 	shift(&params);
 	char *nick = shift(&params);
-	whoLen += snprintf(
-		&whoBuf[whoLen], sizeof(whoBuf) - whoLen,
+	size_t cap = sizeof(who.buf) - who.len;
+	int len = snprintf(
+		&who.buf[who.len], cap,
 		"%s\3%d%s\3",
-		(whoLen ? ", " : ""), color(user), nick
+		(who.len ? ", " : ""), color(user), nick
 	);
+	if ((size_t)len < cap) who.len += len;
 }
 
 static void handle315(char *prefix, char *params) {
 	(void)prefix;
 	shift(&params);
 	char *chan = shift(&params);
-	whoLen = 0;
+	who.len = 0;
 	uiFmt(
 		L"In \3%d%s\3 are %s",
-		color(chan), chan, whoBuf
+		color(chan), chan, who.buf
 	);
 }