From fc6331e279f45dd14d4202d746d6ae536022be9d Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 29 Jan 2016 10:11:47 +1100 Subject: [PATCH] More gracefully handle out of sync/corrupt binary messages by searching memory for EOL instead of strings --- src/ckpool.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index c8866bc7..b8412979 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -713,7 +713,7 @@ int read_socket_line(connsock_t *cs, float *timeout) clear_bufline(cs); recv_available(ckp, cs); // Intentionally ignore return value - eom = strchr(cs->buf, '\n'); + eom = memchr(cs->buf, '\n', cs->bufofs); tv_time(&start); @@ -751,7 +751,7 @@ int read_socket_line(connsock_t *cs, float *timeout) ret = -1; goto out; } - eom = strchr(cs->buf, '\n'); + eom = memchr(cs->buf, '\n', cs->bufofs); tv_time(&now); diff = tvdiff(&now, &start); copy_tv(&start, &now); @@ -764,10 +764,10 @@ int read_socket_line(connsock_t *cs, float *timeout) ret = eom - cs->buf; } else *eom = '\0'; - cs->buflen = cs->buf + cs->bufofs - eom - 1; - if (cs->buflen) - cs->bufofs = eom - cs->buf + 1; - else + if (cs->bufofs > ret + 1) { + cs->buflen = cs->bufofs - ret - 1; + cs->bufofs = ret + 1; + } else cs->bufofs = 0; out: if (ret < 0) {