changeset 20747:0f5809a9f137

lib: safe_mkstemp*() didn't always truncate prefix back to original on failure. This caused repeated safe_mkstemp*() calls with the same prefix to keep increasing its size. It probably didn't really break anything (unless it was called enough many times to reach 255 filename length), but the filenames were still confusingly ugly.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 15 Sep 2016 11:30:15 +0300
parents 6c827dbef85e
children c51e205f7e62
files src/lib/safe-mkstemp.c
diffstat 1 files changed, 13 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/safe-mkstemp.c	Thu Sep 15 10:23:41 2016 +0300
+++ b/src/lib/safe-mkstemp.c	Thu Sep 15 11:30:15 2016 +0300
@@ -65,6 +65,7 @@
 		}
 		i_close_fd(&fd);
 		i_unlink(str_c(prefix));
+		str_truncate(prefix, prefix_len);
 		return -1;
 	}
 	return fd;
@@ -83,13 +84,23 @@
 
 int safe_mkstemp_hostpid(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
 {
+	size_t orig_prefix_len = str_len(prefix);
+	int fd;
+
 	str_printfa(prefix, "%s.%s.", my_hostname, my_pid);
-	return safe_mkstemp(prefix, mode, uid, gid);
+	if ((fd = safe_mkstemp(prefix, mode, uid, gid)) == -1)
+		str_truncate(prefix, orig_prefix_len);
+	return fd;
 }
 
 int safe_mkstemp_hostpid_group(string_t *prefix, mode_t mode,
 			       gid_t gid, const char *gid_origin)
 {
+	size_t orig_prefix_len = str_len(prefix);
+	int fd;
+
 	str_printfa(prefix, "%s.%s.", my_hostname, my_pid);
-	return safe_mkstemp_group(prefix, mode, gid, gid_origin);
+	if ((fd = safe_mkstemp_group(prefix, mode, gid, gid_origin)) == -1)
+		str_truncate(prefix, orig_prefix_len);
+	return fd;
 }