From: Alexander Barton Date: Fri, 1 May 2020 21:55:28 +0000 (+0200) Subject: Handle commands in the read buffer before reading more data X-Git-Tag: rel-26-rc1~15 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=commitdiff_plain;h=54fac576030d7aaf8bce4a982c880fe18680741e Handle commands in the read buffer before reading more data If there are more bytes in the read buffer already than a single valid IRC command can get long (513 bytes, COMMAND_LEN), wait for this/those command(s) to be handled first and don't try to read even more data from the network (which most probably would overflow the read buffer of this connection soon). --- diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index 7d1576e1..ca7030f4 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -734,6 +734,18 @@ Conn_Handler(void) continue; } + if (array_bytes(&My_Connections[i].rbuf) >= COMMAND_LEN) { + /* There is still more data in the read buffer + * than a single valid command can get long: + * so either there is a complete command, or + * invalid data. Therefore don't try to read in + * even more data from the network but wait for + * this command(s) to be handled first! */ + io_event_del(My_Connections[i].sock, + IO_WANTREAD); + continue; + } + io_event_add(My_Connections[i].sock, IO_WANTREAD); } @@ -1554,6 +1566,10 @@ Read_Request(CONN_ID Idx) assert(Idx > NONE); assert(My_Connections[Idx].sock > NONE); + /* Check if the read buffer is "full". Basically this shouldn't happen + * here, because as long as there possibly are commands in the read + * buffer (buffer usage > COMMAND_LEN), the socket shouldn't be + * scheduled for reading in Conn_Handler() at all ... */ #ifdef ZLIB if ((array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN) || (array_bytes(&My_Connections[Idx].zip.rbuf) >= READBUFFER_LEN)) @@ -1561,7 +1577,6 @@ Read_Request(CONN_ID Idx) if (array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN) #endif { - /* Read buffer is full */ Log(LOG_ERR, "Receive buffer space exhausted (connection %d): %d/%d bytes", Idx, array_bytes(&My_Connections[Idx].rbuf), READBUFFER_LEN);