Browse Source

Bugfix: Use memchr to look for newlines in socket line data, since the buffer isn't null terminated

Luke Dashjr 13 years ago
parent
commit
14180f790a
1 changed files with 3 additions and 6 deletions
  1. 3 6
      util.c

+ 3 - 6
util.c

@@ -936,7 +936,7 @@ static bool sock_full(struct pool *pool, bool wait)
 	struct timeval timeout;
 	struct timeval timeout;
 	fd_set rd;
 	fd_set rd;
 
 
-	if (pool->readbuf.buf && strchr(pool->readbuf.buf, '\n'))
+	if (pool->readbuf.buf && memchr(pool->readbuf.buf, '\n', pool->readbuf.len))
 		return true;
 		return true;
 
 
 	FD_ZERO(&rd);
 	FD_ZERO(&rd);
@@ -959,7 +959,7 @@ char *recv_line(struct pool *pool)
 	char *tok, *sret = NULL;
 	char *tok, *sret = NULL;
 	size_t n = 0;
 	size_t n = 0;
 
 
-	while (!(pool->readbuf.buf && strchr(pool->readbuf.buf, '\n'))) {
+	while (!(pool->readbuf.buf && memchr(pool->readbuf.buf, '\n', pool->readbuf.len))) {
 		char s[RBUFSIZE];
 		char s[RBUFSIZE];
 		CURLcode rc;
 		CURLcode rc;
 
 
@@ -979,9 +979,6 @@ char *recv_line(struct pool *pool)
 		}
 		}
 
 
 		len = all_data_cb(s, n, 1, &pool->readbuf);
 		len = all_data_cb(s, n, 1, &pool->readbuf);
-		pool->readbuf.buf = realloc(pool->readbuf.buf, pool->readbuf.len + 1);
-		((char*)pool->readbuf.buf)[pool->readbuf.len] = '\0';
-
 		if (n != (size_t)len) {
 		if (n != (size_t)len) {
 			applog(LOG_DEBUG, "Error appending readbuf in recv_line");
 			applog(LOG_DEBUG, "Error appending readbuf in recv_line");
 			goto out;
 			goto out;
@@ -989,7 +986,7 @@ char *recv_line(struct pool *pool)
 	}
 	}
 
 
 	// Assuming the bulk of the data will be in the line, steal the buffer and return it
 	// Assuming the bulk of the data will be in the line, steal the buffer and return it
-	tok = strchr(pool->readbuf.buf, '\n');
+	tok = memchr(pool->readbuf.buf, '\n', pool->readbuf.len);
 	*tok = '\0';
 	*tok = '\0';
 	len = tok - (char*)pool->readbuf.buf;
 	len = tok - (char*)pool->readbuf.buf;
 	pool->readbuf.len -= len + 1;
 	pool->readbuf.len -= len + 1;