From f78fe91cef9cabd9313dd57fe08d3ad2de4b8270 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 17 Jan 2016 07:54:13 +1100 Subject: [PATCH] Only ever increase buffer size in add_buflen --- src/ckpool.c | 13 ++++++++----- src/ckpool.h | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) 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;