Browse Source

Fix reinsertion logic

master
Con Kolivas 9 years ago
parent
commit
4ef8ab49e3
  1. 31
      src/ckpool.c

31
src/ckpool.c

@ -523,7 +523,8 @@ static void clear_bufline(connsock_t *cs)
cs->bufofs = cs->buflen; cs->bufofs = cs->buflen;
cs->buflen = 0; cs->buflen = 0;
cs->buf[cs->bufofs] = '\0'; cs->buf[cs->bufofs] = '\0';
} } else
cs->bufofs = 0;
} }
static void add_bufline(connsock_t *cs, const char *readbuf, const int len) static void add_bufline(connsock_t *cs, const char *readbuf, const int len)
@ -554,7 +555,7 @@ static int read_cs_length(connsock_t *cs, float *timeout, int len)
{ {
int ret = len; int ret = len;
while (cs->buflen < len) { while (cs->bufofs < len) {
char readbuf[PAGESIZE]; char readbuf[PAGESIZE];
ret = wait_read_select(cs->fd, *timeout); ret = wait_read_select(cs->fd, *timeout);
@ -585,10 +586,14 @@ static int read_gz_line(connsock_t *cs, float *timeout)
ret = -1; ret = -1;
goto out; goto out;
} }
memcpy(&msglen, cs->buf, 4); memcpy(&msglen, cs->buf, 4);
compsize = le32toh(msglen); compsize = le32toh(msglen);
memcpy(&msglen, cs->buf + 4, 4); memcpy(&msglen, cs->buf + 4, 4);
decompsize = le32toh(msglen); decompsize = le32toh(msglen);
/* Remove the gz variables */
cs->buflen = cs->bufofs - 8;
cs->bufofs = 8; cs->bufofs = 8;
clear_bufline(cs); clear_bufline(cs);
@ -606,32 +611,44 @@ static int read_gz_line(connsock_t *cs, float *timeout)
ret = -1; ret = -1;
goto out; goto out;
} }
/* Clear out all the compressed data */
cs->buflen = cs->bufofs - compsize;
cs->bufofs = compsize;
clear_bufline(cs);
/* Do decompresion and buffer reconstruction here */ /* Do decompresion and buffer reconstruction here */
dest = ckalloc(decompsize); dest = ckalloc(decompsize);
res = decompsize; res = decompsize;
ret = uncompress((Bytef *)dest, &res, (Bytef *)cs->buf, compsize); ret = uncompress((Bytef *)dest, &res, (Bytef *)cs->buf, compsize);
/* Clear out all the compressed data */
clear_bufline(cs);
if (ret != Z_OK || res != decompsize) { if (ret != Z_OK || res != decompsize) {
LOGWARNING("Failed to decompress %lu bytes in read_gz_line, got %d", decompsize, ret); LOGWARNING("Failed to decompress %lu bytes in read_gz_line, got %d", decompsize, ret);
ret = -1; ret = -1;
goto out; goto out;
} }
eom = dest + decompsize - 1; eom = dest + decompsize - 1;
if (memcmp(eom, "\n", 1)) { if (memcmp(eom, "\n", 1)) {
LOGWARNING("Failed to find EOM in decompressed data in read_gz_line"); LOGWARNING("Failed to find EOM in decompressed data in read_gz_line");
ret = -1; ret = -1;
goto out; goto out;
} }
*eom = '\0'; *eom = '\0';
ret = decompsize - 1;
/* Wedge the decompressed buffer back to the start of cs->buf */ /* Wedge the decompressed buffer back to the start of cs->buf */
buf = cs->buf; buf = cs->buf;
buflen = cs->buflen; buflen = cs->bufofs;
cs->buf = dest; cs->buf = dest;
dest = NULL; dest = NULL;
ret = cs->buflen = decompsize; cs->bufofs = decompsize;
if (buflen) if (buflen) {
LOGWARNING("Remainder %s", buf);
add_bufline(cs, buf, buflen); add_bufline(cs, buf, buflen);
cs->buflen = buflen;
cs->bufofs = decompsize;
} else
cs->buflen = cs->bufofs = 0;
out: out:
free(dest); free(dest);
return ret; return ret;

Loading…
Cancel
Save