]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: print correct error string on i_close_fd() failure
authorJosef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
Tue, 19 Sep 2017 13:17:45 +0000 (16:17 +0300)
committerTimo Sirainen <tss@dovecot.fi>
Tue, 3 Oct 2017 07:14:05 +0000 (10:14 +0300)
close_keep_errno() preserved the original errno, therefore the %m in
i_close_fd()'s error message printed an unrelated error string.

src/lib/lib.c
src/lib/macros.h

index 4f998ba07df81b873882993ba3dc3cfe88eb059b..c931f62b9607f1090db789d47a936153e2f95f10 100644 (file)
@@ -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 };
index 05879079c7590de38f5df1867b2826ccfd7ca67a..5d75fed7c6a79acdeec64898ebc3a45c3917f4c2 100644 (file)
 /* 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() \