From b8008236d3b03d92701158f755593b1619a6a28c Mon Sep 17 00:00:00 2001 From: Tobias Girstmair Date: Sun, 12 May 2024 20:29:46 +0200 Subject: [PATCH] NULL-terminate read() calls we reuse the samme buffer for all network and local read(2)s. irc_answer (or more specifically, strtok(3)) expects NULL-terminated strings. this caused us to re-parse older strings when the buffer shrank between two consecutive read(2) calls. --- ircpipe.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ircpipe.c b/ircpipe.c index e959259..2c8eff6 100644 --- a/ircpipe.c +++ b/ircpipe.c @@ -214,7 +214,7 @@ int irc_setup(const sock_t sock, const int outfd, const char *nick, const char * /* block until we get a RPL_WELCOME or an error: */ for (;;) { if (poll(fds, 1, POLL_TIMEOUT)) { - n = READ(sock, buf, BUFSIZ); + n = READ(sock, buf, BUFSIZ); buf[n] = '\0'; write(outfd, buf, n); n = irc_answer(sock, buf, NICK); if (n & NICK) break; @@ -230,7 +230,7 @@ int irc_setup(const sock_t sock, const int outfd, const char *nick, const char * /* todo: dedup this block with NICK/RPL_WELCOME */ for (;;) { if (poll(fds, 1, POLL_TIMEOUT)) { - n = READ(sock, buf, BUFSIZ); + n = READ(sock, buf, BUFSIZ); buf[n] = '\0'; write(outfd, buf, n); n = irc_answer(sock, buf, JOIN); if (n & JOIN) break; @@ -258,7 +258,7 @@ int irc_poll(const sock_t sock, const int infd, const int outfd) { /* XXX: long responses don't get fully processed until user input */ /* XXX: must handle TLS_WANT_POLLIN and TLS_WANT_POLLOUT for READ and WRITE! */ if (fds[IRC].revents & POLLIN) { - n = READ(sock, buf, BUFSIZ); + n = READ(sock, buf, BUFSIZ); buf[n] = '\0'; if (n == 0) return -1; /* server closed connection */ fds[IRC].events = POLLIN | (n==TLS_WANT_POLLOUT?POLLOUT:0); write(outfd, buf, n); @@ -266,7 +266,7 @@ int irc_poll(const sock_t sock, const int infd, const int outfd) { /* update last-msg-rcvd here */ } if (fds[CLI].revents & POLLIN) { - n = read(infd, buf, BUFSIZ); + n = read(infd, buf, BUFSIZ); buf[n] = '\0'; if (n == 0) return 0; /* we closed connection */ n = WRITE(sock, buf, n); fds[IRC].events = POLLIN | (n==TLS_WANT_POLLOUT?POLLOUT:0); -- 2.39.3