Permalink
Browse files

Simplify sockgets line parsing

This simplifies the line parsing of sockets, and fixes the bug where s[strlen(s)-1] accesses invalid memory in case of an empty line (s = "\n" and it looks behind to find '\r').
  • Loading branch information...
1 parent 66f6d87 commit d6420e95bd4f9fdaab4f3e5252a6545ca85fbebd @thommey thommey committed with vanosg Feb 4, 2017
Showing with 10 additions and 16 deletions.
  1. +8 −14 src/net.c
  2. +2 −2 src/patch.h
View
@@ -982,26 +982,20 @@ int sockgets(char *s, int *len)
(socklist[i].handler.sock.inbuf != NULL)) {
if (!(socklist[i].flags & SOCK_BINARY)) {
/* look for \r too cos windows can't follow RFCs */
- p = strchr(socklist[i].handler.sock.inbuf, '\n');
- if (p == NULL)
- p = strchr(socklist[i].handler.sock.inbuf, '\r');
+ p = strpbrk(socklist[i].handler.sock.inbuf, "\r\n");
if (p != NULL) {
- *p = 0;
+ while (*p == '\n' || *p == '\r')
+ *p++ = 0;
if (strlen(socklist[i].handler.sock.inbuf) > 510)
socklist[i].handler.sock.inbuf[510] = 0;
strcpy(s, socklist[i].handler.sock.inbuf);
- px = nmalloc(strlen(p + 1) + 1);
- strcpy(px, p + 1);
- nfree(socklist[i].handler.sock.inbuf);
- if (px[0])
+ if (*p) {
+ px = nmalloc(strlen(p) + 1);
+ strcpy(px, p);
+ nfree(socklist[i].handler.sock.inbuf);
socklist[i].handler.sock.inbuf = px;
- else {
- nfree(px);
+ } else
socklist[i].handler.sock.inbuf = NULL;
- }
- /* Strip CR if this was CR/LF combo */
- if (s[strlen(s) - 1] == '\r')
- s[strlen(s) - 1] = 0;
*len = strlen(s);
return socklist[i].sock;
}
View
@@ -39,12 +39,12 @@ patch("Git"); /* Git version */
*
*
*/
-patch("1486143995"); /* current unixtime */
+patch("1486176263"); /* current unixtime */
/*
*
*
*/
-patch("remotewho");
+patch("sockgets");
/*
*
*

0 comments on commit d6420e9

Please sign in to comment.