From 4493d117a4ac88cad42790221f89c0589693ce57 Mon Sep 17 00:00:00 2001 From: donglinpeng Date: Wed, 11 Jan 2017 01:03:14 +0800 Subject: [PATCH] common/buffer:Pipe fd is not closed when set nonblocking fails. Signed-off-by: donglinpeng --- src/common/buffer.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/common/buffer.cc b/src/common/buffer.cc index a1e65c30b4f7e..c62e5aec0525d 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -27,6 +27,7 @@ #include "include/types.h" #include "include/compat.h" #include "include/inline_memory.h" +#include "include/scope_guard.h" #if defined(HAVE_XIO) #include "msg/xio/XioMsg.h" #endif @@ -514,7 +515,7 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; return 0; } - void close_pipe(int *fds) { + static void close_pipe(const int *fds) { if (fds[0] >= 0) VOID_TEMP_FAILURE_RETRY(::close(fds[0])); if (fds[1] >= 0) @@ -536,6 +537,7 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; << bendl; throw error_code(r); } + auto sg = make_scope_guard([=] { close_pipe(tmpfd); }); r = set_nonblocking(tmpfd); if (r < 0) { bdout << "raw_pipe: error setting nonblocking flag on temp pipe: " @@ -552,12 +554,10 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; r = errno; bdout << "raw_pipe: error tee'ing into temp pipe: " << cpp_strerror(r) << bendl; - close_pipe(tmpfd); throw error_code(r); } data = (char *)malloc(len); if (!data) { - close_pipe(tmpfd); throw bad_alloc(); } r = safe_read(tmpfd[0], data, len); @@ -566,10 +566,8 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; << bendl; free(data); data = NULL; - close_pipe(tmpfd); throw error_code(r); } - close_pipe(tmpfd); return data; } bool source_consumed; -- 2.39.5