tm512 avatar tm512 committed ecd339c

Truncate IRC lines if they fill up the client's buffer without containing \r\n.
Ensure that the client's buffer won't overflow.

Comments (0)

Files changed (1)

 		eprint (0, "Exhausted reconnection attempts to %s:%s, giving up.", cl->host, cl->port);
 	}
 
-	// Free the server's resources before returning
+	// Free the client's resources before returning
 	// Set the pointer to NULL so we give a free slot back
 	free (cl->host);
 	free (cl->port);
 int irc_getln (ircclient_t *cl, char *buf)
 {
 	int ret = 0;
-	char *pos = NULL;
+	char *pos = NULL, *tmpos = NULL;
 	char tmpbuf [MAXBUF] = { 0 };
 
 	// Clear the buffer we're sent
 	memset (buf, 0, MAXBUF);
 
-	while ((pos = strstr (cl->rbuf, "\r\n")) == NULL)
+	while ((tmpos = strstr (cl->rbuf, "\r\n")) == NULL && ret < MAXBUF - strlen (cl->rbuf) - 1)
 	{
 		memset (tmpbuf, 0, MAXBUF);
 
-		ret = net_recv (cl->s, tmpbuf, MAXBUF - strlen (cl->rbuf));
+		ret = net_recv (cl->s, tmpbuf, MAXBUF - strlen (cl->rbuf) - 1);
 
 		if (ret <= 0)
 			return ret;
 		memcpy (cl->rbuf + strlen (cl->rbuf), tmpbuf, MAXBUF - strlen (cl->rbuf));
 	}
 
+	// See if we need to truncate the line
+	// If the entire buffer filled before we got an \r\n, add it ourselves
+	if (tmpos)
+		pos = tmpos;
+	else
+	{
+		cl->rbuf [MAXBUF - 2] = '\n';
+		cl->rbuf [MAXBUF - 3] = '\r';
+		pos = cl->rbuf + MAXBUF - 3;
+	}
+
 	// Copy line to buf, add \0
 	memcpy (buf, cl->rbuf, pos + 2 - cl->rbuf);
 	strstr (buf, "\r\n") [2] = '\0';
 	memcpy (tmpbuf, pos + 2, MAXBUF - (pos + 2 - cl->rbuf));
 	memcpy (cl->rbuf, tmpbuf, MAXBUF);
 
+	// Seems hacky... Recursively call irc_getln again if we didn't get a
+	// complete line, and there is the rest of the line waiting to be read.
+	if (!tmpos)
+		irc_getln (cl, tmpbuf);
+
 	return strlen (buf);
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.