dovecot-2.2: lib: file_create_locked() was leaking fds and locks.

dovecot at dovecot.org dovecot at dovecot.org
Sun May 24 17:26:35 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/1d088dc567bd
changeset: 18743:1d088dc567bd
user:      Timo Sirainen <tss at iki.fi>
date:      Sun May 24 13:22:56 2015 -0400
description:
lib: file_create_locked() was leaking fds and locks.

diffstat:

 src/lib/file-create-locked.c |  19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)

diffs (53 lines):

diff -r f98aad82ddda -r 1d088dc567bd src/lib/file-create-locked.c
--- a/src/lib/file-create-locked.c	Sat May 23 19:21:10 2015 -0400
+++ b/src/lib/file-create-locked.c	Sun May 24 13:22:56 2015 -0400
@@ -18,6 +18,7 @@
 		  struct file_lock **lock_r, const char **error_r)
 {
 	struct stat st1, st2;
+	int ret;
 
 	if (fstat(fd, &st1) < 0) {
 		*error_r = t_strdup_printf("fstat(%s) failed: %m", path);
@@ -27,14 +28,19 @@
 				 set->lock_timeout_secs, lock_r, error_r) <= 0)
 		return -1;
 	if (stat(path, &st2) == 0) {
-		return st1.st_ino == st2.st_ino &&
+		ret = st1.st_ino == st2.st_ino &&
 			CMP_DEV_T(st1.st_dev, st2.st_dev) ? 1 : 0;
 	} else if (errno == ENOENT) {
-		return 0;
+		ret = 0;
 	} else {
 		*error_r = t_strdup_printf("stat(%s) failed: %m", path);
-		return -1;
+		ret = -1;
 	}
+	if (ret <= 0) {
+		/* the fd is closed next - no need to unlock */
+		file_lock_free(lock_r);
+	}
+	return ret;
 }
 
 static int
@@ -96,15 +102,14 @@
 		fd = open(path, O_RDWR);
 		if (fd != -1) {
 			ret = try_lock_existing(fd, path, set, lock_r, error_r);
-			if (ret < 0) {
-				i_close_fd(&fd);
-				return -1;
-			}
 			if (ret > 0) {
 				/* successfully locked an existing file */
 				*created_r = FALSE;
 				return fd;
 			}
+			i_close_fd(&fd);
+			if (ret < 0)
+				return -1;
 		} else if (errno != ENOENT) {
 			*error_r = t_strdup_printf("open(%s) failed: %m", path);
 			return -1;


More information about the dovecot-cvs mailing list