dovecot: Fixed a race condition causing cache corruption when cr...

dovecot at dovecot.org dovecot at dovecot.org
Sun Oct 28 14:31:36 EET 2007


details:   http://hg.dovecot.org/dovecot/rev/f3ca0b9e3628
changeset: 6646:f3ca0b9e3628
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Oct 28 14:31:13 2007 +0200
description:
Fixed a race condition causing cache corruption when creating/compressing
cache file.

diffstat:

1 file changed, 14 insertions(+), 11 deletions(-)
src/lib-index/mail-cache-transaction.c |   25 ++++++++++++++-----------

diffs (57 lines):

diff -r ce7f9d33d566 -r f3ca0b9e3628 src/lib-index/mail-cache-transaction.c
--- a/src/lib-index/mail-cache-transaction.c	Sun Oct 28 13:27:46 2007 +0200
+++ b/src/lib-index/mail-cache-transaction.c	Sun Oct 28 14:31:13 2007 +0200
@@ -74,7 +74,7 @@ static void mail_cache_transaction_reset
 	mail_index_ext_set_reset_id(ctx->trans, ctx->cache->ext_id,
 				    ctx->cache_file_seq);
 
-	if (ctx->cache_data)
+	if (ctx->cache_data != NULL)
 		buffer_set_used_size(ctx->cache_data, 0);
 	if (array_is_created(&ctx->cache_data_seq))
 		array_clear(&ctx->cache_data_seq);
@@ -116,8 +116,11 @@ static int mail_cache_transaction_lock(s
 	if (ctx->cache_file_seq == 0) {
 		if (!ctx->cache->opened)
 			(void)mail_cache_open_and_verify(ctx->cache);
-		if (!MAIL_CACHE_IS_UNUSABLE(ctx->cache))
+		if (!MAIL_CACHE_IS_UNUSABLE(ctx->cache)) {
+			i_assert(ctx->cache_data == NULL ||
+				 ctx->cache_data->used == 0);
 			ctx->cache_file_seq = ctx->cache->hdr->file_seq;
+		}
 	}
 
 	if ((ret = mail_cache_lock(ctx->cache, FALSE)) <= 0)
@@ -507,15 +510,6 @@ mail_cache_transaction_flush(struct mail
 		buffer_set_used_size(ctx->cache_data, ctx->prev_pos);
 	}
 
-	if (ctx->cache_file_seq == 0) {
-		if (!ctx->cache->opened)
-			(void)mail_cache_open_and_verify(ctx->cache);
-		if (MAIL_CACHE_IS_UNUSABLE(ctx->cache))
-			return -1;
-
-		ctx->cache_file_seq = ctx->cache->hdr->file_seq;
-	}
-
 	if (ctx->cache_file_seq != ctx->cache->hdr->file_seq) {
 		/* cache file reopened - need to abort */
 		mail_cache_transaction_reset(ctx);
@@ -788,6 +782,15 @@ void mail_cache_add(struct mail_cache_tr
 	    (MAIL_CACHE_DECISION_NO | MAIL_CACHE_DECISION_FORCED))
 		return;
 
+	if (ctx->cache_file_seq == 0) {
+		if (!ctx->cache->opened)
+			(void)mail_cache_open_and_verify(ctx->cache);
+		if (MAIL_CACHE_IS_UNUSABLE(ctx->cache))
+			return;
+
+		ctx->cache_file_seq = ctx->cache->hdr->file_seq;
+	}
+
 	file_field = ctx->cache->field_file_map[field_idx];
 	if (file_field == (uint32_t)-1) {
 		/* we'll have to add this field to headers */


More information about the dovecot-cvs mailing list