diff --git a/src/ckpool.c b/src/ckpool.c index 4f8177f7..889414cc 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -518,9 +518,10 @@ void empty_buffer(connsock_t *cs) * only unprocessed data of bufofs length. */ static void clear_bufline(connsock_t *cs) { - if (unlikely(!cs->buf)) + if (unlikely(!cs->buf)) { cs->buf = ckzalloc(PAGESIZE); - else if (cs->buflen) { + cs->bufsize = PAGESIZE; + } else if (cs->buflen) { memmove(cs->buf, cs->buf + cs->bufofs, cs->buflen); memset(cs->buf + cs->buflen, 0, cs->bufofs); cs->bufofs = cs->buflen; @@ -532,14 +533,16 @@ static void clear_bufline(connsock_t *cs) static void add_buflen(connsock_t *cs, const char *readbuf, const int len) { int backoff = 1; - size_t buflen; + int buflen; char *newbuf; buflen = round_up_page(cs->bufofs + len + 1); - while (42) { + while (cs->bufsize < buflen) { newbuf = realloc(cs->buf, buflen); - if (likely(newbuf)) + if (likely(newbuf)) { + cs->bufsize = buflen; break; + } if (backoff == 1) fprintf(stderr, "Failed to realloc %d in read_socket_line, retrying\n", (int)buflen); cksleep_ms(backoff); diff --git a/src/ckpool.h b/src/ckpool.h index cc3a7968..84285a3d 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -76,9 +76,12 @@ struct connsock { char *url; char *port; char *auth; + char *buf; int bufofs; int buflen; + int bufsize; + ckpool_t *ckp; /* Semaphore used to serialise request/responses */ sem_t sem;