From c1f812effcd1d722727c563391bfe5c84c11e998 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 14 Dec 2015 16:10:46 +1100 Subject: [PATCH] Fix extremely unlikely race on fd being accessed before it is set to -1 on closing --- src/libckpool.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libckpool.c b/src/libckpool.c index 0c1b891e..4c315dfb 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -620,13 +620,17 @@ void block_socket(int fd) void _close(int *fd, const char *file, const char *func, const int line) { + int sockd; + if (*fd < 0) return; - LOGDEBUG("Closing file handle %d", *fd); - if (unlikely(close(*fd))) - LOGWARNING("Close of fd %d failed with errno %d:%s from %s %s:%d", - *fd, errno, strerror(errno), file, func, line); + sockd = *fd; + LOGDEBUG("Closing file handle %d", sockd); *fd = -1; + if (unlikely(close(sockd))) { + LOGWARNING("Close of fd %d failed with errno %d:%s from %s %s:%d", + sockd, errno, strerror(errno), file, func, line); + } } int bind_socket(char *url, char *port)