From af191b4364ebecef36f0fdf120fa99273c134000 Mon Sep 17 00:00:00 2001 From: Alex Denes Date: Sat, 9 Jan 2021 21:28:10 +0000 Subject: [PATCH] BUGFIX: Don't overwrite the FIFO buffer we're parsing --- src/main.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main.c b/src/main.c index 62ff16f..8e3a348 100644 --- a/src/main.c +++ b/src/main.c @@ -153,7 +153,6 @@ main(int argc, char* argv[]) setup_signals(); for (;;) { ctime = time(NULL); - LOG(LOG_DEBUG, "Current time is: %lu", ctime); if (connection.info.state == CONN_IDLE) nanosleep(&sleep, NULL); if (!run || connection.info.state == CONN_CLOSING) { signed long temp; @@ -287,28 +286,32 @@ main(int argc, char* argv[]) } if (buf.fifo.fd != -1) { ssize_t brd, len; - /* Buffer writer */ + /* FIFO reader */ if ((brd = read(buf.fifo.fd, buf.fifo.buffer + buf.fifo.append_pos, sizeof(buf.fifo.buffer) - buf.fifo.append_pos - 1)) > 0) { *(buf.fifo.buffer + (buf.fifo.append_pos += (size_t) brd)) = '\0'; - LOG(LOG_DEBUG, "Read %li bytes from FIFO. Now appending at %li", brd, buf.fifo.append_pos); + LOG(LOG_DEBUG, + "Read %li bytes from FIFO buffer. Now appending at %li with \"%s\" so far.", + brd, + buf.fifo.append_pos, + buf.fifo.buffer); + connection.info.state = CONN_ACTIVE; } else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { - LOG(LOG_ERROR, "Failed to read FIFO fifo. " ERRNOFMT, strerror(errno), errno); + LOG(LOG_ERROR, "Failed to read FIFO buffer. " ERRNOFMT, strerror(errno), errno); connection.info.state = CONN_RECONNECTING; continue; } memset((void*) &buffer, '\0', sizeof(IRC_Message)); if ((len = get_buffer_line(buf.fifo.buffer)) > 0) { - LOG(LOG_DEBUG, "Got FIFO message: %s", buf.fifo.buffer); + LOG(LOG_DEBUG, "Got IRC message: %s", buf.fifo.buffer); if (Tok_mesg(buf.fifo.buffer, &buffer) == 1) { - LOG(LOG_DEBUG, "%s", "Tokenized FIFO message successfully."); signed long temp; - if ((temp = Assm_mesg(buf.fifo.buffer, &buffer, sizeof(buf.fifo.buffer))) > 0) { - if (flush_buffer(buf.fifo.buffer, (size_t) temp, buf.send.fd) == -1) { + if ((temp = Assm_mesg(buf.send.buffer, &buffer, sizeof(buf.send.buffer))) > 0) { + if (flush_buffer(buf.send.buffer, (size_t) temp, buf.send.fd) == -1) { LOG(LOG_WARN, - "Couldn't send FIFO fifo to " ADDRFMT ". " ERRNOFMT, + "Couldn't send FIFO contents to " ADDRFMT ". " ERRNOFMT, connection.data.address, connection.data.service, strerror(errno), @@ -317,18 +320,17 @@ main(int argc, char* argv[]) continue; } } - for (long unsigned int x = 0; x < sizeof(buf.fifo.buffer) && *(buf.fifo.buffer + len + x); x++) - *(buf.fifo.buffer + x) = *(buf.fifo.buffer + x + len); - buf.fifo.append_pos -= (unsigned long) len; - *(buf.fifo.buffer + buf.fifo.append_pos) = '\0'; - } else - LOG(LOG_WARN, "%s", "Received invalid IRC message (see RFC2812)."); - } else if (len == -1) { - connection.info.state = CONN_RECONNECTING; - } else if (buf.fifo.append_pos == sizeof(buf.fifo.buffer) - 1) { - LOG(LOG_WARN, "%s. %s.", "FIFO buffer is full and no message could be parsed", "Discarding buffer"); + } else { + LOG(LOG_WARN, "%s", "Received invalid IRC message on FIFO (see RFC2812)."); + } + for (long unsigned int x = 0; x < sizeof(buf.fifo.buffer) && *(buf.fifo.buffer + len + x); x++) + *(buf.fifo.buffer + x) = *(buf.fifo.buffer + x + len); + buf.fifo.append_pos -= (unsigned long) len; + *(buf.fifo.buffer + buf.fifo.append_pos) = '\0'; + } else if (buf.fifo.append_pos == sizeof(buf.fifo.fd) - 1) { + LOG(LOG_WARN, "%s.", "FIFO buffer is full and no message could be parsed. Cleared buffer."); + memset(buf.fifo.buffer, '\0', sizeof(buf.fifo)); buf.fifo.append_pos = 0; - *buf.fifo.buffer = '\0'; } } else { char pathbuf[UIRCD_LIMITS_PATH];