From: Josef 'Jeff' Sipek Date: Tue, 19 Sep 2017 13:17:45 +0000 (+0300) Subject: lib: print correct error string on i_close_fd() failure X-Git-Tag: 2.3.0.rc1~947 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b93dba1fdb1850e3c2706a40fd78450d555d8d9a;p=thirdparty%2Fdovecot%2Fcore.git lib: print correct error string on i_close_fd() failure close_keep_errno() preserved the original errno, therefore the %m in i_close_fd()'s error message printed an unrelated error string. --- diff --git a/src/lib/lib.c b/src/lib/lib.c index 4f998ba07d..c931f62b96 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -41,6 +41,23 @@ int close_keep_errno(int *fd) return ret; } +void i_close_fd_real(int *fd, const char *file, int line) +{ + int saved_errno; + + if (*fd == -1) + return; + + i_assert(*fd > 0); + + saved_errno = errno; + if (unlikely(close(*fd) < 0)) + i_error("close(%d[%s:%d]) failed: %m", *fd, file, line); + errno = saved_errno; + + *fd = -1; +} + void fd_close_maybe_stdio(int *fd_in, int *fd_out) { int *fdp[2] = { fd_in, fd_out }; diff --git a/src/lib/macros.h b/src/lib/macros.h index 05879079c7..5d75fed7c6 100644 --- a/src/lib/macros.h +++ b/src/lib/macros.h @@ -212,14 +212,8 @@ /* Close the fd and set it to -1. This assert-crashes if fd == 0, and is a no-op if fd == -1. Normally fd == 0 would happen only if an uninitialized fd is attempted to be closed, which is a bug. */ -#define i_close_fd(fd) STMT_START { \ - if (*fd != -1) { \ - i_assert(*fd > 0); \ - if (unlikely(close_keep_errno(fd) < 0)) \ - i_error("close(%d[%s:%d]) failed: %m", \ - *(fd), __FILE__, __LINE__); \ - } \ - } STMT_END +void i_close_fd_real(int *fd, const char *file, int line); +#define i_close_fd(fd) i_close_fd_real((fd), __FILE__, __LINE__) #ifndef STATIC_CHECKER # define i_unreached() \