From bbfadc3e47f9d22195045c6cd9c658deed22c8ed Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Wed, 19 Nov 2014 21:34:40 +1100 Subject: [PATCH] Store the fd in read_socket_line to not have it change under us, and empty the cs buffer on failure --- src/ckpool.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 630a7721..c7ff6fee 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -367,11 +367,11 @@ void empty_buffer(connsock_t *cs) * of the buffer for use on the next receive. */ int read_socket_line(connsock_t *cs, int timeout) { + int fd = cs->fd, ret = -1; char *eom = NULL; size_t buflen; - int ret = -1; - if (unlikely(cs->fd < 0)) + if (unlikely(fd < 0)) goto out; if (unlikely(!cs->buf)) @@ -388,7 +388,7 @@ int read_socket_line(connsock_t *cs, int timeout) while (42) { char readbuf[PAGESIZE] = {}; - ret = wait_read_select(cs->fd, eom ? 0 : timeout); + ret = wait_read_select(fd, eom ? 0 : timeout); if (eom && !ret) break; if (ret < 1) { @@ -398,7 +398,7 @@ int read_socket_line(connsock_t *cs, int timeout) LOGERR("Select failed in read_socket_line"); goto out; } - ret = recv(cs->fd, readbuf, PAGESIZE - 4, 0); + ret = recv(fd, readbuf, PAGESIZE - 4, 0); if (ret < 1) { LOGERR("Failed to recv in read_socket_line"); ret = -1; @@ -423,6 +423,7 @@ int read_socket_line(connsock_t *cs, int timeout) *eom = '\0'; out: if (ret < 0) { + empty_buffer(cs); dealloc(cs->buf); Close(cs->fd); }